题目描述:
统计数组 arr 中值等于 item 的元素出现的次数
// 7.统计数组 arr 中值等于 item 的元素出现的次数
function count(arr,item){
var count = 0;
arr.forEach((i)=>{
if(i == item){
count ++;
}
})
return count;
}
console.log(count([1, 2, 4, 4, 3, 4, 3], 4)); // 结果: 3
题目描述:
为数组 arr 中的每个元素求二次方。不要直接修改数组 arr,结果返回新的数组
// 9.为数组 arr 中的每个元素求二次方。不要直接修改数组 arr,结果返回新的数组
function square(arr){
var newArr = [];
arr.forEach((item)=>{
newArr.push(item*item);
})
return newArr;
}
console.log(square([1, 2, 3, 4])); // 结果: [1, 4, 9, 16]
---------继续奥。(*^▽^*)