数组,字符串,数学函数


问答

  • 数组方法里push、pop、shift、unshift、join、split分别是什么作用。
  • push:可以在数组的末尾增加一个元素
  • pop:可以在数组的末尾拿走一个元素
  • shift:从数组的开头取出元素
  • unshift:从数组的开头加入元素
  • join:把数组元素(对象调用其toString()方法)使用参数作为连接符连接成一字符串,不会修改原数组内容。
  • split:用于把一个字符串分割成字符串数组。括号里的参数会从字符串中被移除,返回存进一个数组当中的子字符串。如果忽略参数,则返回原字符串。如果参数是一个空字符串,则原字符串将被转换为由字符串中字符组成的一个数组。

代码

  • 用 splice 实现 push、pop、shift、unshift方法
数组,字符串,数学函数_第1张图片
用 splice 实现 push、pop、shift、unshift方法

  • 使用数组拼接出如下字符串
var prod = { name: '女装', styles: ['短款', '冬季', '春装']};
function getTpl(data){//todo...
};
var result = getTplStr(prod); //result为下面的字符串
女装
短款
冬季
春装
数组,字符串,数学函数_第2张图片
使用数组拼接出如下字符串

代码预览


  • 写一个find函数,实现下面的功能
var arr = [ "test", 2, 1.5, false ]
find(arr, "test") // 0
find(arr, 2) // 1
find(arr, 0) // -1
数组,字符串,数学函数_第3张图片
find函数

代码预览


  • 写一个函数filterNumeric,把数组 arr 中的数字过滤出来赋值给新数组newarr, 原数组arr不变
arr = ["a", "b", 1, 3, 5, "b", 2];
newarr = filterNumeric(arr); // [1,3,5,2]
数组,字符串,数学函数_第4张图片
函数filterNumeric

代码预览


  • 对象obj有个className属性,里面的值为的是空格分割的字符串(和html元素的class特性类似),写addClass、removeClass函数,有如下功能
var obj = { 
className: 'open menu'
}
addClass(obj, 'new') // obj.className='open menu new'
addClass(obj, 'open') // 因为open已经存在,所以不会再次添加open
addClass(obj, 'me') // me不存在,所以 obj.className变为'open menu new me'
console.log(obj.className) // "open menu new me"
removeClass(obj, 'open') // 去掉obj.className里面的 open,变成'menu new me
'removeClass(obj, 'blabla') // 因为blabla不存在,所以此操作无任何影响
数组,字符串,数学函数_第5张图片
addClass、removeClass函数

代码预览


  • 写一个camelize函数,把my-short-string形式的字符串转化成myShortString形式的字符串,如
camelize("background-color") == 'backgroundColor'
camelize("list-style-image") == 'listStyleImage'
数组,字符串,数学函数_第6张图片
转换

代码预览


  • 如下代码输出什么?为什么?
arr = ["a", "b"];
arr.push( function() { 
alert(console.log('hello hunger valley'))
    } );//push把函数 alert(console.log('hello hunger valley')) 放入arr数组末尾
arr[arr.length-1]() // 输出数组最后一个console.log('hello hunger valley')
数组,字符串,数学函数_第7张图片
结果

  • 写一个函数filterNumericInPlace,过滤数组中的数字,删除非数字。要求在原数组上操作
arr = ["a", "b", 1, 3, 4, 5, "b", 2];
//对原数组进行操作,不需要返回值
filterNumericInPlace(arr);
console.log(arr) // [1,3,4,5,2]
数组,字符串,数学函数_第8张图片
Paste_Image.png

  • 写一个ageSort函数实现数组中对象按age从小到大排序
var john = { name: "John Smith", age: 23 }
var mary = { name: "Mary Key", age: 18 }
var bob = { name: "Bob-small", age: 6 }
var people = [ john, mary, bob ]
ageSort(people) // [ bob, mary, john ]
数组,字符串,数学函数_第9张图片
ageSort


  • 写一个filter(arr, func) 函数用于过滤数组,接受两个参数,第一个是要处理的数组,第二个参数是回调函数(回调函数遍历接受每一个数组元素,当函数返回true时保留该元素,否则删除该元素)。实现如下功能:
function isNumeric (el){ return typeof el === 'number'; }
arr = ["a",3,4,true, -1, 2, "b"]
arr = filter(arr, isNumeric) ; // arr = [3,4,-1, 2], 过滤出数字
arr = filter(arr, function(val) { return typeof val === "number" && val > 0 }); // arr = [3,4,2] 过滤出大于0的整数
数组,字符串,数学函数_第10张图片
Image.png

代码预览


字符串

  • 写一个 ucFirst函数,返回第一个字母为大写的字符
ucFirst("hunger") == "Hunger"
数组,字符串,数学函数_第11张图片
首字母大写

代码预览


  • 写一个函数truncate(str, maxlength), 如果str的长度大于maxlength,会把str截断到maxlength长,并加上...,如
truncate("hello, this is hunger valley,", 10)) == "hello, thi...";
truncate("hello world", 20)) == "hello world"
数组,字符串,数学函数_第12张图片
截断

代码预览


数学函数

  • 写一个函数,获取从min到max之间的随机整数,包括min不包括max
function rand1(min,max){
 return Math.floor(Math.random()*(max-min))+min;
}
  • 写一个函数,获取从min都max之间的随机整数,包括min包括max
function rand2(min, max) {
 return Math.floor(Math.random() * (max - min + 1))+min;
}
  • 写一个函数,获取一个随机数组,数组中元素为长度为len,最小值为min,最大值为max(包括)的随机整数
function arrRand(len,min,max){
 var arr = [];
 for(var i=0; i
  • 写一个函数,生成一个长度为 n 的随机字符串,字符串字符的取值范围包括0到9,a到 z,A到Z。
function getRandStr(len){ 
//todo...
}
var str = getRandStr(10); // 0a3iJiRZap
数组,字符串,数学函数_第13张图片
随机字符串

代码预览


              本文版权归作者饥人谷_Josh和饥人谷所有,转载请注明来源

你可能感兴趣的:(数组,字符串,数学函数)