从JavaScript中的数字列表等于或大于数字
我们需要编写一个JavaScript函数,该函数将数字数组作为第一个参数,并将单个数字作为第二个参数。该函数应返回一个数组,其中包含输入数组中所有大于或等于作为第二个参数的数字的元素。
示例
以下是代码-
const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5];
const threshold = 40;
const findGreater = (arr, num) => {
const res = [];
for(let i = 0; i < arr.length; i++){
if(arr[i] < num){
continue;
};
res.push(arr[i]);
};
return res;
};
console.log(findGreater(arr, threshold));输出结果
这将在控制台中产生以下输出-
[ 56, 76, 45, 56 ]