Lenocam
Lenocam

Reputation: 341

Why am I getting "undefined" after I otherwise get the output I was aiming to get with nested for loops in JavaScript?

This function's out put should just be the largest number in each array. Something in the code is adding undefined to the end of my output.

Is biggest being output one more time then the loops run through the arrays? What is my error and how do I correct it?

Function:

function largestOfFour(arr) {
  var biggest = 0;
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > biggest) {
        biggest = arr[i][j];
      }

    }
    console.log(biggest);
  }

}

Test:

console.log(largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]
  ]));

Expected Ouput:

5
27
39
1001

Actual Output:

5
27
39
1001
undefined

What is causing the undefined output? When I put arr.length-1 or arr[i].length-1 it eliminated the last loop, but not undefined.

Edit:

I added an array to push my returned values into. This allowed me to get rid of console.log in my code. I was confused by not being able to use return without stopping my loops, so I defaulted to console.log without really understanding the consequences.

Here is my new code:

    function largestOfFour(arr) {
  var biggest = 0;
  var answerArray = [];
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > biggest) {
        biggest = arr[i][j];
      }
    }
    answerArray.push(biggest);
  }
  return answerArray;
}

Upvotes: 0

Views: 73

Answers (4)

Dalin Huang
Dalin Huang

Reputation: 11342

Because there is nothing returned in your function call, console log can't find anything to print.

An updated version using return

function largestOfFour(arr) {
  var big_arr = [];
  var biggest = 0;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > biggest) {
        biggest = arr[i][j];
      }
      if(i === arr.length - 1 && j === arr[i].length - 1){
         big_arr.push(biggest);
         return big_arr;
      }
    }
    big_arr.push(biggest);
  }
}


console.log(largestOfFour([
  [4, 5, 1, 3],
  [13, 27, 18, 26],
  [32, 35, 37, 39],
  [1000, 1001, 857, 1]
]));

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386883

You could use Math.max and a spread syntax ....

function largestOfFour(array) {
    return array.map(v => Math.max(...v));
}

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

Upvotes: 2

adeneo
adeneo

Reputation: 318372

The function should return the result, and using Math.max seems a lot easier

function largestOfFour(arr) {
    return arr.map( x => Math.max.apply(Math, x) );
}

console.log(largestOfFour([
  [4, 5, 1, 3],
  [13, 27, 18, 26],
  [32, 35, 37, 39],
  [1000, 1001, 857, 1]
]));

Upvotes: 3

Tom O.
Tom O.

Reputation: 5941

It's because you're using console.log when you call the function as well as in the loop. Check the code below:

function largestOfFour(arr) {
  var biggest = 0;
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > biggest) {
        biggest = arr[i][j];
      }

    }
    console.log(biggest);
  }

}

//console.log(
largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]
  ])
  //);

Upvotes: 2

Related Questions