0

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
   }
  }
 }
}
3
  • Are you trying to sort an array? What is the code supposed to do? Commented Oct 22, 2016 at 1:32
  • 1
    your largest function will loop n squared times (where n is array.length) ... to get the largest value in an array only requires a loop n long - so, no you're not doing it right ... also, lookup Math.max for an even simpler solution Commented Oct 22, 2016 at 1:33
  • The easiest way to 'know if you're doing it right' is to try a simpler example, and see whether it works. Commented Oct 22, 2016 at 1:33

5 Answers 5

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]));

Sign up to request clarification or add additional context in comments.

Comments

3

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

Note the code assumes array.length > 0. Usually I prefer initializing max = -Infinity and starting the loop at i=0.
Wep, didn't check this one. But I assume that OP will take care of this. (Ahah, i'll try to update my answers)
Thanks so much for your answer.
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

0

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

0

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]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.