有数组 var arr= [5,8,4,8,2,0,5,1,41,29,6,0,2,10,11],请编写函数将数组中大于等于10的元素选出来,放入新数组newarr;
时间: 2023-10-06 16:13:28 浏览: 91
function selectGreaterEqualTen(arr) {
var newarr = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= 10) {
newarr.push(arr[i]);
}
}
return newarr;
}
var arr = [5, 8, 4, 8, 2, 0, 5, 1, 41, 29, 6, 0, 2, 10, 11];
var newarr = selectGreaterEqualTen(arr);
console.log(newarr); // [ 10, 11, 41, 29 ]
相关问题
有数组 var arr=[5,8,4,8,2,0,5,1,41,29,6,0,2,10,11],请编写函数将数组中大于等于10的元素选出来,放入新数组newarr
function selectOverTen(arr) {
var newarr = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= 10) {
newarr.push(arr[i]);
}
}
return newarr;
}
var arr = [5, 8, 4, 8, 2, 0, 5, 1, 41, 29, 6, 0, 2, 10, 11];
var newarr = selectOverTen(arr);
console.log(newarr); // 输出 [41, 29, 10, 11]
}; 点击复制后,将打开C知道体验页 | 有数组 var arr= [5,8,4,8,2,0,5,1,41,29,6,0,2,10,11],请编写函数将数组中大于等于10的元素选出来,放入新数组newarr;在HBuilder上
可这样实现:
function selectOverTen(arr){
var newarr = [];
for(var i=0; i<arr.length; i++){
if(arr[i]>=10){
newarr.push(arr[i]);
}
}
return newarr;
}
var arr= [5,8,4,8,2,0,5,1,41,29,6,0,2,10,11];
var result = selectOverTen(arr);
console.log(result); // [41, 29, 10, 11]
阅读全文