'Find the biggest number in an array by using JavaScript loops

Create a function called biggestNumberInArray().

That takes an array as a parameter and returns the biggest number.

Here is an array

const array = [-1, 0, 3, 100, 99, 2, 99]

What I try in my JavaScript code:

 function biggestNumberInArray(arr) {
   for (let i = 0; i < array.length; i++) {
      for(let j=1;j<array.length;j++){
          for(let k =2;k<array.length;k++){
              if(array[i]>array[j] && array[i]>array[k]){
                    console.log(array[i]);
           }
         }
      }
   }
}

It returns 3 100 99.

I want to return just 100 because it is the biggest number.

Is there a better way to use loops to get the biggest value?

Using three different JavaScript loops to achieve this (for, forEach, for of, for in).

You can use three of them to accomplish it.



Solution 1:[1]

zer00ne's answer should be better for simplicity, but if you still want to follow the for-loop way, here it is:

function biggestNumberInArray (arr) {
    // The largest number at first should be the first element or null for empty array
    var largest = arr[0] || null;

    // Current number, handled by the loop
    var number = null;
    for (var i = 0; i < arr.length; i++) {
        // Update current number
        number = arr[i];

        // Compares stored largest number with current number, stores the largest one
        largest = Math.max(largest, number);
    }

    return largest;
}

Solution 2:[2]

Some ES6 magic for you, using the spread syntax:

function biggestNumberInArray(arr) {
  const max = Math.max(...arr);
  return max;
}

Actually, a few people have answered this question in a more detailed fashion than I do, but I would like you to read this if you are curious about the performance between the various ways of getting the largest number in an array.

Solution 3:[3]

There are multiple ways.

  1. Using Math max function

let array = [-1, 10, 30, 45, 5, 6, 89, 17];
console.log(Math.max(...array))
  1. Using reduce

let array = [-1, 10, 30, 45, 5, 6, 89, 17];
console.log(array.reduce((element,max) => element > max ? element : max, 0));
  1. Implement our own function

let array = [-1, 10, 30, 45, 5, 6, 89, 17];

function getMaxOutOfAnArray(array) {
  let maxNumber = -Infinity;
  array.forEach(number => { maxNumber =  number > maxNumber ? number :  maxNumber; })
  console.log(maxNumber);
}

getMaxOutOfAnArray(array);

Solution 4:[4]

The simplest way is using Math.max.apply:

const array = [-1,0,3,100, 99, 2, 99];

function biggestNumberInArray(arr) {
  return Math.max.apply(Math, arr);
}

console.log(biggestNumberInArray(array));

If you really want to use a for loop, you can do it using the technique from this answer:

const array = [-1,0,3,100, 99, 2, 99];

function biggestNumberInArray(arr) {
  var m = -Infinity,
    i = 0,
    n = arr.length;
  for (; i != n; ++i) {
    if (arr[i] > m) {
      m = arr[i];
    }
  }
  return m;
}

console.log(biggestNumberInArray(array));

And you could also use reduce:

const array = [-1,0,3,100, 99, 2, 99];

function biggestNumberInArray(array) {
  return array.reduce((m, c) => c > m ? c : m);
}

console.log(biggestNumberInArray(array));

Solution 5:[5]

I think you misunderstand how loops are used - there is no need to have three nested loops. You can iterate through the array with a single loop, keeping track of the largest number in a variable, then returning the variable at the end of the loop.

function largest(arr) {
var largest = arr[0]
arr.forEach(function(i) {
  if (i > largest){
    largest = i 
  }
}
return largest;
}

Of course you can do this much more simply: Math.max(...arr) but the question does ask for a for loop implementation.

Solution 6:[6]

This is best suited to some functional programming and using a reduce, for loops are out of favour these days.

const max = array => array && array.length ? array.reduce((max, current) => current > max ? current : max) : undefined;

console.log(max([-1, 0, 3, 100, 99, 2, 99]));

This is 70% more performant than Math.max https://jsperf.com/max-vs-reduce/1

Solution 7:[7]

Another visual way is to create a variable called something like maxNumber, then check every value in the array, and if it is greater than the maxNumber, then the maxNumber now = that value.

const array = [-1,0,3,100, 99, 2, 99];

function biggestNumberInArray(arr) {
    let maxNumber;
    for(let i = 0; i < arr.length; i++){
        if(!maxNumber){ // protect against an array of values less than 0
            maxNumber = arr[i]
        }
        if(arr[i] > maxNumber){
            maxNumber = arr[i];
        }
    }
    return maxNumber
}
console.log(biggestNumberInArray(array));

I hope this helps :)

Solution 8:[8]

var list = [12,34,11,10,34,68,5,6,2,2,90];
var length = list.length-1;
for(var i=0; i<length; i++){
    for(j=0; j<length; j++){
        if(list[j]>list[j+1]){
                [ list[j] , list[j+1] ] = [ list[j+1] , list[j] ];
        }
    }
}
console.log(list[list.length-1]);

Solution 9:[9]

You Can try My codes to find the highest number form array using for loop.

  
function largestNumber(number){
    let max = number[0];
    for(let i = 0; i < number.length; i++){
        let element = number[i];
        if(element > max){
            max = element;
        }
    }
    return max;
}
let arrayNum= [22,25,40,60,80,100];
let result = largestNumber(arrayNum);
console.log('The Highest Number is: ',result);

Solution 10:[10]

let arr = [1,213,31,42,21];
let max = 0;

for(let i = 0; i < arr.length; i++) {
    if(arr[i] > max) {
    max = arr[i]
  } 
}

console.log(max)

Solution 11:[11]

There are multiple ways.

way - 1 | without for loop

const data = [-1, 0, 3, 100, 99, 2, 99];

// way - 1 | without for loop
const maxValue = Math.max(...data);
const maxIndex = data.indexOf(maxValue);
console.log({ maxValue, maxIndex }); // { maxValue: 100, maxIndex: 3 }

way - 2 | with for loop

const data = [-1, 0, 3, 100, 99, 2, 99];

// way - 2 | with for loop
let max = data[0];
for (let i = 0; i < data.length; i++) {
    if (data[i] > max) {
        max = data[i];
    }
}
console.log(max); // 100

Solution 12:[12]

const array = [-1, 0, 3, 100, 99, 2, 99] let longest = Math.max(...array);

Solution 13:[13]

what about this

const array = [1, 32, 3, 44, 5, 6]
console.time("method-test")
var largestNum = array[0]
for(var i = 1; i < array.length; i++) {
  largestNum = largestNum > array[i] ? largestNum : array[i]  
}
console.log(largestNum)
console.timeEnd("method-test")