如何在数组中找到最大的一组数字并分别用javascript返回? [英] How to find the largest group of numbers in an array and return them separately in javascript?
本文介绍了如何在数组中找到最大的一组数字并分别用javascript返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何制作仅返回大于输入数字的数字的函数? 我的代码在这里不起作用,我也不知道为什么.
How can I make a function that returns only the numbers greater than the number that I entered? My code here isn't working, and I don't know why.
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var num = Number(prompt('number'));
function findBiggestNumbers(num) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] > num) {
num = arr[i];
}
}
return num;
// }
console.log(findBiggestNumbers(num));
推荐答案
要使用数组,您可以使用filter
函数,它会返回带有一定条件的数组子集.因此,您可以轻松做到:
To work with arrays you could use the filter
function, it returns a subset of the array with some condition. So, you can simpley do:
var num = 5; //using 5 as an example
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = a.filter(number => number > num);
您可以将其放入函数中.
You can put this into an function.
这篇关于如何在数组中找到最大的一组数字并分别用javascript返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文