Array and for loops

I was solving a problem that required me to write code that outputs the following array [[1], [2, 2], [3, 3, 3]] . I tried writing code in python and it worked as I expected , but when I did it in javascript the result was unanticipated. This is my code in JS, hope you can show me what I’m doing wrong:

let finalArr = [];

  for (var i = 1; i < 3 + 1; i++) {

    let subArr = []

    for (var j = 0; j < i; j++) {





What was the unanticipated result? More details helps us help you.

I suspect you are pushing to the final array in a place that doesn’t make sense.


finalArr.push(subArr); looks like it is in the wrong location. It’s currently part of the inner for loop and should be at the end of the outer for loop after the inner loops closing curly brace ‘}’.


That’s right. I don’t believe I made such mistake. I guess I got mixed up with the indentation method of python in blocking code rather than the curly braces.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.