JavaScript: greatest value in an array

Viewed 198

The function only reurn the greatest number if it is at first (arr[0]), or last index (arr[arr.length-1]) of the array. How do I fix the logical error?

function isGreater(arr) {
 let a = arr[0];
 for (let i = 0; i < arr.length; i++) {
  var isGreat = (a < arr[i]) ? arr[i] : a;
 }
 return isGreat;
}
console.log(isGreater([7, 9, 10000, 11, 25, 20]));
// output=> 20, expected output 10000
console.log(isGreater([7, 11, 25, 2]));
// output=> 7, expected output 25

6 Answers

The logic error is that you're assigning either arr[i] or a to isGreat, but never comparing isGreat to arr[i].

Just reuse a rather than using isGreat:

function isGreater(arr) {
    let a = arr[0];
    for (let i = 1; i < arr.length; i++) {
        a = a < arr[i] ? arr[i] : a;
    }
    return a;
}
console.log(isGreater([7, 9, 10000, 11, 25, 20]));
console.log(isGreater([7, 11, 25, 2]));

Note that I updated that to skip the first index in the loop, since a already contains that value.

This line:

a = a < arr[i] ? arr[i] : a;

could be this if you prefer:

if (a < arr[i]) {
    a = arr[i];
}

I assume this is an exercise. If not, you can get your result more directly like this:

// ES2015+
function isGreater(arr) {
    return Math.max(...arr);
}

or this:

// ES5 and earlier
function isGreater(arr) {
    return Math.max.apply(Math, arr);
}

You need to take the first value of the array as actually greatest value and start the loop from index 1. No need for a, another reference to the array.

Inside the loop check if isGreat is smaller than the actual value value and update isGreat.

The check prevents to assign a superfluous assignment of the old and new greatest value.

function isGreater(arr) {
    let isGreat = arr[0];
    for (let i = 1; i < arr.length; i++) {
        if (isGreat < arr[i]) {
            isGreat = arr[i];
        }
    }
    return isGreat;
}

console.log(isGreater([7, 9, 10000, 11, 25, 20])); // 10000
console.log(isGreater([7, 11, 25, 2]));            //    25

You should do this:

function fun(){
   let arr = [7, 9, 10000, 11, 25, 20];
 let a = arr[0];
 for (let i = 0; i < arr.length; i++) {
  a = (a < arr[i]) ? arr[i] : a;
 }
 return a; //Returns 10000
}

First off, you defined isGreat inside of a loops, so it will be redefined every iteration. Try this:

function isGreater(arr){
       let greatest = arr[0];
       for(let i = 0; i < arr.length; i++){
          if(arr[i] > greatest){
            greatest = arr[i];
          }
       }
       return greatest;
}
    
console.log(isGreater([0, 4, 6, 1, 3]))

When you are finding the greatest, you should try to compare current value with current greatest value so the greatest one will change across the iteration.

this is the example

function isGreater(arr) {
  let greatest = arr[0];
  for (let i = 1; i < arr.length; i++) {
    greatest = greatest < arr[i] ? arr[i] : greatest;
  }
  return greatest;
}

Although, you may already have better answers, I did it like this:

const isGreater = arr => {
    return arr.sort((a, b) => b - a)[0];
};

This just sorts the array in a descending order, and returns the first element of the newly ordered array.

Related