lundi 15 novembre 2021

What's the job of "return" right underneath the if clause in this exact function? also pls explain in words how this function works ty

Write a function that returns the smallest number that’s greater or equal to 0. Especially that return right underneath if (num < 0). What's that return's job here?

function minNonNegative(numArray) {
  let min = Infinity;

  numArray.forEach(function(num) {
    if (num < 0) {
      return;
    } else if (num < min) {
      min = num
    }
  });
  return min;
}

console.log(minNonNegative([-3,2,5,-9]))

Aucun commentaire:

Enregistrer un commentaire