newarr = filterNumberic(arr); // [1,3,5,2]
console.log(newarr)
方法二:
arr = ["a", 1,3,5, "b", 2];
function isNumber(element) {
return typeof element === 'number';
}
var newarr = arr.filter(isNumber)
console.log(newarr)
对象obj有个className属性,里面的值为的是空格分割的字符串(和html元素的class特性类似),写addClass、removeClass函数,有如下功能:
var obj = {className: 'open menu'}addClass(obj, 'new') // obj.className='open menu new'addClass(obj, 'open') // 因为open已经存在,此操作无任何办法addClass(obj, 'me') // obj.className='open menu new me'console.log(obj.className) // "open menu new me"
removeClass(obj, 'open') // obj.className='menu new me' removeClass(obj, 'blabla') // 不变
代码:
var obj = {className: 'open menu'}var addClass = function(a,b){var name = a.className.split(" ");if(name.indexOf(b) === -1) {name.push(b);}else{console.log("因为"+b+"已经存在,此操作无任何办法");}a.className = name.join(" ");console.log('obj.className='+a.className);}var removeClass = function(a,b){var name = a.className.split(" ");if(name.indexOf(b) !== -1){name.splice(name.indexOf(b),1)a.className = name.join(" ");console.log('obj.className='+a.className)}else{console.log('不变')}}
addClass(obj, 'new') // obj.className='open menu new' addClass(obj, 'open') // 因为open已经存在,此操作无任何办法 addClass(obj, 'me') // obj.className='open menu new me' console.log(obj.className) // "open menu new me" removeClass(obj, 'open') // obj.className='menu new me' removeClass(obj, 'blabla') // 不变
写一个camelize函数,把my-short-string形式的字符串转化成myShortString形式的字符串,如:
camelize("background-color") == 'backgroundColor'
camelize("list-style-image") == 'listStyleImage'
代码:
function camelize(string){
return string.replace(/-/g,'')
}
console.log(camelize("background-color"))
camelize("background-color") == 'backgroundColor'
camelize("list-style-image") == 'listStyleImage'
如下代码输出什么?为什么?
arr = ["a", "b"];
arr.push( function() { alert(console.log('hello hunger valley')) } );
arrarr.length-1 // ?
因为arr.push( function() { alert(console.log('hello hunger valley')) } );将function() { alert(console.log('hello hunger valley')push到arr[]最后一位,arr[arr.length-1]()取该数组最后一位,然后立即执行该函数,由于function() { alert(console.log('hello hunger valley')中console.log只允许在控制台中打开,所以结果为undefined。
写一个函数filterNumericInPlace,过滤数组中的数字,删除非数字
arr = ["a", 1,3,4,5, "b", 2];
//对原数组进行操作,不需要返回值
filterNumericInPlace(arr);
console.log(arr) // [1,3,4,5,2]
代码:
arr = ["a","d", 1,3,4,5, "b", 2];
//对原数组进行操作,不需要返回值
function filterNumericInPlace(data){
for(i=0;i写一个ageSort函数实现如下功能:
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 ]
代码:
方法一:
function ageSort(arr){
arr.sort(function(a,b){return a.age-b.age})
return arr
}
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 ]
console.log(ageSort(people))
方法二:
function ageSort(a){
for(i=0;i0){
var b = a[i];
a[i] = a[j];
a[j] = b;
}
}
}
return a
}
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 ]
console.log(ageSort(people))
写一个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 val > 0 }); // arr = [2] 过滤出大于0的整数
代码:
function filter(data,callback){return data.filter(callback)}
function isNumeric (el){ return typeof el === 'number'; } arr = ["a",3,4,true, -1, 2, "b"] arr = filter(arr, isNumeric) ; // arr = [3,4,-1, 2], 过滤出数字 console.log(arr) arr = filter(arr, function(val) { return val > 0 }); // arr = [2] 过滤出大于0的整数 console.log(arr)
字符串
写一个 ucFirst函数,返回第一个字母为大写的字符。
ucFirst("hunger") == "Hunger"
代码:
function ucFirst(string){
return string[0].toUpperCase()+string.slice(1);
}
console.log(ucFirst("hunger"))
ucFirst("hunger") == "Hunger"
写一个函数truncate(str, maxlength), 如果str的长度大于maxlength,会把str截断到maxlength长,并加上...,如:
truncate("hello, this is hunger valley,", 10)) == "hello, thi...";
truncate("hello world", 20)) == "hello world"
代码:
function truncate(str,maxlength){
if(str.length>maxlength){
var sub = str.substring(maxlength)
str = str.replace(sub,'...');
} return str;
}
console.log(truncate("hello, this is hunger valley,", 10));
truncate("hello, this is hunger valley,", 10) == "hello, thi...";
truncate("hello world", 20) == "hello world"
数学函数
写一个函数limit2,保留数字小数点后两位,四舍五入,如:
var num1 = 3.456
limit2( num1 ); //3.46
limit2( 2.42 ); //2.42
代码:
var num1 = 3.456
function limit2(data){
var num = Math.round(data*100);
return num/100
}
limit2( num1 ); //3.46
limit2( 2.42 ); //2.42
console.log(limit2(num1));
console.log(limit2(2.42));
console.log(limit2(-1.15555555))
写一个函数,获取从min到max之间的随机数,包括min不包括max。
代码:
function fun(min,max){
return min+Math.random()*(max-min)
}
console.log(fun(5,10))
写一个函数,获取从min都max之间的随机整数,包括min包括max。
代码:
function fun(min,max){
return Math.Round(min+Math.random()*(max-min))
}
console.log(fun(5,10))
写一个函数,获取一个随机数组,数组中元素为长度为len,最小值为min,最大值为max(包括)的随机数 .
代码:
function fun(min,max,leng){
var arr = []
for(i=0;i相信看了本文案例你已经掌握了方法,更多精彩请关注 第一PHP社区 其它相关文章!
相关阅读:
JS的闭包与定时器
JS的Dom与事件小结
以上就是Javascript数组-字符串-数学函数的详细内容,更多请关注 第一PHP社区 其它相关文章!