1
\$\begingroup\$

Is this a good way to solve the quiz "Chessboard" from http://eloquentjavascript.net/02_program_structure.html ?

Write a program that creates a string that represents an ×ばつ8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a "#" character. The characters should form a chess board.

When you have a program that generates this pattern, define a variable size = 8 and change the program so that it works for any size, outputting a grid of the given width and height.

This is my code:

 size = 10; 
 
 grid = ""
 
 for (var i = 1; i <= size; i++) {
 for (var j = 1; j <= size; j++) {
 if (i % 2 === 0) {
 	grid+= "# "
 } else {
 	grid+= " #"
 }
 }
 grid+= "\n"
 }
 
 
 console.log(grid)

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Aug 7, 2017 at 16:50
\$\endgroup\$
2
  • \$\begingroup\$ You put a '#' and a space in each 'cell'. The problem you posted says space OR '#'. So for each iteration of the inner loop, it should be grid += "#"; or grid += " "; \$\endgroup\$ Commented Aug 7, 2017 at 17:12
  • \$\begingroup\$ This code produces a board that is larger than the size specified in the challenge. \$\endgroup\$ Commented Aug 7, 2017 at 19:58

1 Answer 1

5
\$\begingroup\$

Fun question;

  • you should write a function that takes a parameter instead of just writing the code

  • A chessboard has lots of repetition, take a minute to ponder how String.repeat could make this code much simpler.

  • Your indentation is not perfect, consider using a site like http://jsbeautifier.org/

  • I am not a big fan of var within the loop, I would declare var up front.

This is a possible solution that provides the right size of the board:

function createChessboardString(size){
 const line = ' #'.repeat( size ),
 even = line.substring(0,size),
 odd = line.substring(1,size+1);
 let out = '';
 while(size--){
 out = out + ((size % 2) ? odd: even ) + '\n';
 }
 return out;
}
console.log(createChessboardString(8));

You could consider for very large boards that the board in essence repeats odd + '\n' + even, so you could repeat that as well. The problem for me is that there are too many corner cases to consider. So personally I would go for the above for any board size < 1000.

answered Aug 7, 2017 at 17:33
\$\endgroup\$
5
  • \$\begingroup\$ one question: I changed the code to (i + j) % 2 === 0 because i took a look in the solution. Could you please explain me this? Why adding these together? @konijn \$\endgroup\$ Commented Aug 7, 2017 at 18:12
  • 1
    \$\begingroup\$ I would have to see the surrounding code, plus that is not really what codereview is about. I was just thinking that your code seems wrong actually. If ' ' is white and '#'' is black, then your board is way too big. \$\endgroup\$ Commented Aug 7, 2017 at 18:21
  • \$\begingroup\$ @konijn why are you not a fan of var within the loop? \$\endgroup\$ Commented Aug 7, 2017 at 20:17
  • \$\begingroup\$ Cant find back the exact location, but it's in there: github.com/airbnb/javascript#variables basically js hoists those variables anyway to the top \$\endgroup\$ Commented Aug 7, 2017 at 22:56
  • \$\begingroup\$ Your codeuses very incon sistentspacing. \$\endgroup\$ Commented Oct 25, 2019 at 1:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.