The goal is to get the highest value in an array. I hope someone could walk me through where I went wrong with my code, and how I can get it to work.

What I am trying to accomplish is starting with a preset array, comparing Arr[i] and Arr[i+1], then Arr[i+1] and Arr[i+2] and so on... pushing the highest value of each into an empty array called Arrl. Then set Arr equal to Arrl. Then calling the function back, and repeating until only Arr[0] is left and display the result.

This is the code I have so far:

var Arr=[10,56,76,98,82,45,98];
function largernumber(Arr){

var Arrl=[];
while (Arr.length>1);
for (i=0; i<Arr.length; i++){

    if(Arr[i]>Arr[i+1] || Arr[i]===Arr[i+1]){
        Arrl.push(Arr[i]);
    }
    else{
        ArrL.push(Arr[i+1]);
    }
    console.log(Arrl);  
}

for(var ar=0 ; ar<Arrl.length ; ar++){ 
    Arr[ar]=Arrl[ar];
    }
    return largernummber(Arr);
}

I am not great at describing so I will try to show what my plan is. var Arr=[10,56,76,98,82,45,98] (after each pass through the array length will decrease by one)

1st pass-through var Arr=[60,76,98,98,82,98]
2nd var Arr=[76,98,98,98,98]
3rd var Arr=[98,98,98,98]
nth.. var Arr=[98]

Currently my logic is so flawed, its freezing the browser. oops... if possible I'd also love to see where I can place the console log to see each iteration.

5 Answers

0
Maaz Syed Adeeb On Best Solutions

While all the other answers have pointed out perfectly correct and actually simpler solutions to your problem, I'm just "fixing" your code to use the algorithm that you've mentioned. I've avoided any changes to your code that are unnecessary.

Aside from typos in variable names, these are the issues with the code as it is:

  1. while (Arr.length > 1); just loops forever. I assume it was an attempt to find the base case of the recursion. It should be if (Arr.length === 1) return Arr[0];, which says "if there is only one element left, that is the largest and return it".
  2. When accessing i + 1th element in an array, always be careful about accessing the last element. In your case, your loop goes till i < Arr.length and i + 1 === Arr.length for the last iteration which results in undefined. Comparing numbers with undefined won't work. So make sure you loop till the last but one element, using Arr.length - 1.
  3. Finally, you don't actually use the newly created array for the next leg of the recursion. You need to change return largernumber(Arr); to return largernumber(Arrl);

var Arr = [10, 56, 76, 98, 82, 45, 98];

function largernumber(Arr) {
  var Arrl = [];
  if (Arr.length === 1) { return Arr[0] };
  for (i = 0; i < Arr.length - 1; i++) {

    if (Arr[i] > Arr[i + 1] || Arr[i] === Arr[i + 1]) {
      Arrl.push(Arr[i]);
    } else {
      Arrl.push(Arr[i + 1]);
    }
  }

  for (var ar = 0; ar < Arrl.length; ar++) {
    Arr[ar] = Arrl[ar];
  }
  return largernumber(Arrl);
}

console.log(largernumber(Arr));

As you might have noticed by now, point 1 and 3 point out the places where there is infinite recursion.

0
John Kennedy On

If what you are trying to do is getting the maximum number in the provided array then you can simply use Math.max() function for that. See below:

const Arr = [10, 56, 76, 98, 82, 45, 98];
const max = Math.max.apply(null, Arr)
console.log(max)

And in the case that you want your code fixed then see below:

const Arr = [10, 56, 76, 98, 82, 45, 98];

function largerNumber(Arr) {
  const maximums = [];
  
  // Base case
  if (Arr.length == 1) return Arr[0];
  
  // Note the use of Arr.length - 1 instead
  for (let i = 0; i < Arr.length - 1; i++) {
    if (Arr[i] > Arr[i + 1] || Arr[i] === Arr[i + 1]) {
      maximums.push(Arr[i]);
    } else {
      maximums.push(Arr[i + 1]);
    }
  }

  for (let ar = 0; ar < maximums.length; ar++) {
    Arr[ar] = maximums[ar];
  }
  return largerNumber(maximums);
}

console.log(largerNumber(Arr));

0
Maxi Diaz On

Just use Math.max(). Given a list of values it returns the max value.

var Arr=[10,56,76,98,82,45,98]; 

console.log(Math.max(...Arr))

0
Ele On

There are several alternatives. The following code snippet uses the function reduce and thru a Math.max you can check for the greatest value.

let arr = [10, 56, 76, 98, 82, 45, 98];
let max = arr.reduce((a, n) => Math.max(a, n), Number.MIN_SAFE_INTEGER);

console.log(max);

0
Brian Peacock On

The in-build JavaScript Math.max() method can help you achieve your goal as it returns the, erm, maximum value of 2 or more numbers.

It can be used in conjunction with the Array.reduce() method, which applies a function to an array that compares or processes each item down to a single value or property....

var arr = [10,56,76,98,82,45,98];
var max = arr.reduce(function(a, b) {
    return Math.max(a, b);
});
// max = 98

Alternatively you can apply() the Math function directly to the array...

var arr = [10,56,76,98,82,45,98];
var max = Math.max.apply(null, arr);
// max = 98

The Array.reduce() method can be useful in many different situations and on arrays which contain different kinds of data. The Math.max.apply() method only works on arrays of numbers.

The in-built Math.min() method works exactly the same, but returns the lowest value of 2 or more numbers.

Hope that helps. ;)

See also: