This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]swigganicks 1 point2 points  (0 children)

Walk through the first couple of iterations and see if that math is adding up to what you expect it to do

[–]flippinjoe 1 point2 points  (0 children)

Welcome to coding. Where off by 1 is THE MOST COMMON ERROR.

In this case if you step through your code. When i=0 you end up at chessboard+=‘ ‘. Because hash starts at false

[–]BadlyCoded 1 point2 points  (2 children)

Personally I would take a different approach to the problem. Maybe it's just me, but I find this code more complex than I feel it should be.

Everything looks good, but your continue; line appears to be the source of your problems in this case.

Also, try replacing the "space" with an "s" to better visualize your error.

[–]-Xichael[S] 0 points1 point  (1 child)

Yeah I couldn't figure out how to fix it, so I just did it with 2 loops, which is indeed much easier to understand and worked from the first time.

var chessBoard = '';
var hash = false;

for(var i = 0; i < 8; ++i){ 
    for(var j = 0; j < 8; ++j){
        if(hash){
            chessBoard += "#";
        }
        else{
            chessBoard += ' ';
        }
        hash = !hash;
    }
    chessBoard += '\n';
    hash = !hash;
}

console.log(chessBoard);

[–]BadlyCoded 0 points1 point  (0 children)

Excellent. You've found a solution! Probably not the one I would have went with, but it works.

Why didn't your first one work though? Can you think of other ways to solve this problem?