2

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
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
opeolluwa
  • 96
  • 8
  • 1
    Try `Math.max.apply(null, [7, 9, 10000, 11, 25, 20])`. – Nithish Sep 30 '20 at 15:27
  • 1
    `Math.max.apply(null, [7, 9, 10000, 11, 25, 20])` But in any case, you need to have a variable, set to 0 before the loop, that is set to the larger of itself or the current array element in each loop. – Heretic Monkey Sep 30 '20 at 15:28
  • 1
    Does this answer your question? [Finding largest integer in an array in JavaScript](https://stackoverflow.com/questions/13794225/finding-largest-integer-in-an-array-in-javascript) – Heretic Monkey Sep 30 '20 at 15:29
  • 1
    I think it's cleaner to use the spread operator than `Function.prototype.apply`. In this case, `Math.max(...[7, 9, 10000, 11, 25, 20])` Ultimately, performance varies between browsers and both can cause a stack overflow with large arrays, so it's really a matter of personal preference. – ElectricShadow Sep 30 '20 at 15:33

6 Answers6

4

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);
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

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
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

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
}
1

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

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;
}
Hadi KAR
  • 384
  • 3
  • 11
1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Olivier Girardot
  • 389
  • 4
  • 16