Hello you beautiful coders! Today I've been working on a little quiz called "Chessboard" but ran into a weird situation. but my code is spitting out results vertically but I need to make it horizontally so that I can repeat this process a few more times to make a board out of it. Here's my code:
let value = 8;
for (i = 1; i <= value ; i++) {
if ( i % 2 == 0 ) {
let x = i;
console.log(x);
} else {
let y = " ";
console.log(y);
}
}
this above code outputs
2
4
6
8
How can I make these results show horizontally like so:
2 4 6 8
So that I can repeat this process in a new line afterward? I've been thinking about this for awhile but.... I might just be too dumb to figure this out?
I THINK I have to nest this if function again in this function to achieve this but not sure how... Thanks in advance!!
Add an empty string at the beginning of the code, append the results to it, and log it to console, as illustrated below:
var output = '';
let value = 8;
for (i = 1; i <= value ; i++) {
if (i % 2 == 0) {
output += i;
}
output += ' ';
}
console.log(output);