Unnest Nested For Loop

Unnest Nested For Loop
0

#1

Tell us what’s happening:
Did you know you can unnest nested loops?

Your code so far


function addAll(arr) {
  var product = 1;
  // Only change code below this line

  // NOW WATCH THIS!
  for (let i = 0, j = 0; i < arr.length; j = j + 1 === arr[i].length ? 0 * ++i : j + 1) product += arr[i][j];
  
  // Only change code above this line
  return product;
}

// Modify values below to test your code
addAll([[1,2],[3,4],[5,6,7]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops


#2

Just because you can do something, it does not mean that you should!

That is making my eyes bleed :slight_smile:


#3

Strange, my brain finds it incredibly readable. It turns the statement into a sentence that you just need to read from left to right, rather than the whole system of multiline code. If I would ‘speak’ solution it would pretty much match this sentence, rather than a system.

(Of course in real scenario, I would go simpler arr.flat().reduce())


#4

If this is how you speak, remind me not to get caught chatting with you in the kitchen at a party :wink:

tbh, I mentally resist for-loops these days unless it’s a dire emergency - it’s probably just my brain throwing up defenses.

If I saw this line of code at work though, I’m pretty sure I’d throw my laptop out of the window.