Home > database >  Finding minimum number in an array in javascript
Finding minimum number in an array in javascript

Time:08-08

As a noob i am trying to find the min number from an array.

function findLowest(number) {
  for (let i = 0; i < number.length; i  ) {
    const index = i;
    const element = number[i];
    let lowest = element;


    if (element < lowest) {
      lowest = element;
    }
  }
  return lowest;
}

height = [10, 16, 25, 2];
result = findLowest(height);
console.log(result);

The error message is:

ReferenceError: lowest is not defined

Please explain the error message.

CodePudding user response:

    function findLowest(number) {
        lowest =  number[0];
        for (let i = 1; i < number.length; i  ) {
            let element = number[i];
            if (element < lowest) {
                lowest = element;
            }
        }
        return lowest;
    }

    height = [10, 16, 25, 2];
    result = findLowest(height);
    console.log(result);

your code snippet problems:

  1. const index = i; is not used and please remove it.
  2. please don't use const for element because it isn't a constant number and let is better there
  3. what is minimum number? a number that is minimum among all numbers... so for first step, assume first element is minimum and run a for loop from second element to last and if an element is less than your assume, choose that.
lowest =  number[0];

and

if (element < lowest) {
    lowest = element;
}
  1. let lowest = element; this line is harmful and it replace lowest with element in every step and so you missed minimum in a step which the element is not lower...

CodePudding user response:

You define lowest inside the for loop. You can't access it outside the loop. Take a look at this: https://www.digitalocean.com/community/tutorials/understanding-variables-scope-hoisting-in-javascript.

CodePudding user response:

You only have to use Math.min:

console.log(Math.min(...[10, 16, 25, 2]));

CodePudding user response:

It's a scoping issue. On let:

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used

You're re-declaring lowest in the for/loop block but return cannot see it.

If you initialise lowest with the first element of the array first you can minimise the amount of code you write in the loop. I would also add a condition to check whether the input is an array, and has length, so you can short-circuit the function if the condition isn't met.

(And you should ensure that you declare all of your other variables properly with let or const.)

function findLowest(arr) {

  // Check that the input is an array,
  // and that it is not empty
  if (!Array.isArray(arr) || !arr.length) {
    return 'Invalid input';
  }

  // Initialise `lowest` with the
  // first array element so it has a value
  let lowest = arr[0];

  // If the current element < `lowest`
  // update `lowest`
  for (let i = 0; i < arr.length; i  ) {
    if (arr[i] < lowest) {
      lowest = arr[i];
    }
  }

  return lowest;

}

const arr1 = [10, 100, 6, 2, 16, 25];
const result1 = findLowest(arr1);

console.log(result1);

const arr2 = [];
const result2 = findLowest(arr2);

console.log(result2);

  • Related