I have defined a function named larger to find the larger number between two arguments (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
}
}
}
}
5 Answers 5
Just use 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:
function larger(num1, num2) {
return num1 > num2 ? num1 : num2;
}
function largest(array) {
return array.reduce(larger, -Infinity);
}
console.log(largest([5,-2,7,6]));
Comments
The simpliest solution is tu use a max tmp value. 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
I didn't check the length of the array, the above solution work only if the array.length> 0.
Otherwise, you'll have to check it usingsomething like this instead of 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.
3 Comments
array.length > 0. Usually I prefer initializing max = -Infinity and starting the loop at i=0.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));
Comments
If you are just trying to find the maximal value, you should go with Oriol's answer: 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
}
Comments
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]));
Math.maxfor an even simpler solution