[英]Calling a function inside another function in JavaScript
I have defined a function named larger to find the larger number between two arguments (num1, num2). 我定义了一个名为Larger的函数,以在两个参数(num1,num2)之间找到较大的数字。 Now I want to use this function inside another function called "largest" which gets an array and return the largest number of that array, but I`ve got stuck. 现在,我想在另一个称为“最大”的函数中使用此函数,该函数获取一个数组并返回该数组的最大数量,但是我被卡住了。 Can anybody help me with that? 有人可以帮我吗? Here is my codes: 这是我的代码:
function larger(num1, num2){
var largerNumber = 0;
if (num1 > num2){
largerNumber = num1;
} else {
largerNumber = num2;
}
return largerNumber;
}
function largest(array){
for (var i = 0; i < array.length ; i++){
for (var j = 0; j < array.length ; j++){
if (array[i] != array[j]){
//I don`t know if I am doing it right
}
}
}
}
Just use Math.max
: 只需使用Math.max
:
function largest(array) { return Math.max.apply(Math, array); } console.log(largest([5,-2,7,6]));
If you really want to use a custom binary larger
function, consider [].reduce
: 如果您确实想使用自定义的二进制larger
函数,请考虑[].reduce
:
function larger(num1, num2) { return num1 > num2 ? num1 : num2; } function largest(array) { return array.reduce(larger, -Infinity); } console.log(largest([5,-2,7,6]));
The simpliest solution is tu use a max tmp value. 最简单的解决方案是使用最大tmp值。 This way, you only need to do one iteration over all your array. 这样,您只需要对所有数组进行一次迭代。
function largest(array){
var max = array[0];
for (var i = 1; i < array.length ; i++) { // So we start at 1
max = larger(max, array[i]);
// Or use this : if(array[i] > max) max = array[i];
}
AS SAID BY ORIOL ORIOL表示
I didn't check the length of the array, the above solution work only if the array.length > 0. 我没有检查数组的长度,仅当array.length> 0时,上述解决方案才有效。
Otherwise, you'll have to check it usingsomething like this instead of var max = array[0];
否则,您将必须使用类似以下内容的方法进行检查,而不是使用var max = array[0];
function largest(array){
var max = -Infinity;
for (var i = 0; i < array.length ; i++) { Start at 0
max = larger(max, array[i]);
}
It really depends on the ergonomics of your IHM. 这实际上取决于您的IHM的人体工程学。
Iterate through the array once, keeping only the largest: 遍历数组一次,仅保留最大的数组:
function larger(num1, num2){ var largerNumber = 0; if (num1 > num2){ largerNumber = num1; } else { largerNumber = num2; } return largerNumber; } function largest(array){ let largestNumber = array[0]; for (var i = 1; i < array.length ; i++){ largestNumber = larger(largestNumber, array[i]); } return largestNumber; } var test = [1, 53, 352, 22, 351, 333, 123, 5, 25, 96]; console.log(largest(test));
If you are just trying to find the maximal value, you should go with Oriol's answer: Math.max
如果您只是想找到最大值,则应使用Oriol的答案: Math.max
If you are looking for a way to call one function from another you can do it like this: 如果您正在寻找一种从另一个函数调用一个函数的方法,则可以这样进行:
function first_function() {
//code of first function
//call to the other function
second_function();
}
function second_function() {
//code of second function
}
Set the first element of array to a variable, if next element in array is greater set variable to that element; 如果数组中的下一个元素更大,则将数组的第一个元素设置为一个变量;如果数组中的下一个元素更大,则将变量设置为该元素; continue process, return variable 继续过程,返回变量
function largest(array) { if (!array.length) return; for (var i = 0, curr = void 0; i < array.length ; i++) { if (curr === void 0) curr = array[i]; else if (curr < array[i]) curr = array[i]; } return curr } console.log(largest([2,20,2000,2,7]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.