Ask coding questions

← Back to all posts
Basic Checkerboard Problem
ColeAmodeo (1)

Trying out a Javascript challenge where you create a function that can receive a height and width as arguments and then log the appropriate checkerboard pattern.

Cannot locate the issue that makes it infinite loop and crash every time it's run

Answered by OmniShift (12) [earned 5 cycles]
View Answer
Commentshotnewtop
OmniShift (12)

You need to use a different variable for the checkers iterator than you use for the other 2 iterators. At the moment, you're changing the i from checkers with the iterators from oddWidth and evenWidth, since they are nested.

Also, when ran like that you'll notice your next issue: line1 and line2 don't flush with each height. You'll have to reset them with every iteration of checkers.

ColeAmodeo (1)

@OmniShift Thank you, did not take into account the nested loops interfering each other's iterator. have a great day!

timmy_i_chen (1011)

@ColeAmodeo One more thing I'd note is that you can use the same variable but in JS you need to use let to declare them, otherwise they are globally scoped - meaning, they represent the same variable everywhere. if you use let e.g. let i = 1; i <= width; i++, then the variable i will only exist within the for loop it was defined in, making it incapable of interfering with an i that exists in, say, another function.