Skip to main content
added 263 characters in body
Source Link

To take it even a little further, move this out of the inner loop and get rid of the test at all. As you always are going to append a line-break after each row, this is sufficient:

result += "\n";

Thanks to 200_success for pointing that out.

var size = 8,
    result = "";

for (var i = 0; i < size; i++) {
    for (var j = 0; j < size; j++) {
        result += (i + j) % 2 ? "◻" : "◼";
 
        if (size - 1 == j) {}
            result += "\n";
        }
    }
}

console.log(result);
var size = 8,
    result = "";

for (var i = 0; i < size; i++) {
    for (var j = 0; j < size; j++) {
        result += (i + j) % 2 ? "◻" : "◼";
 
        if (size - 1 == j) {
            result += "\n";
        }
    }
}

console.log(result);

To take it even a little further, move this out of the inner loop and get rid of the test at all. As you always are going to append a line-break after each row, this is sufficient:

result += "\n";

Thanks to 200_success for pointing that out.

var size = 8,
    result = "";

for (var i = 0; i < size; i++) {
    for (var j = 0; j < size; j++) {
        result += (i + j) % 2 ? "◻" : "◼";
    }
    result += "\n";
}

console.log(result);
edited body
Source Link

Simply separateRemove the two statements andfirst else branch it will work.

Personally, I would move this part to the end of the loop, as it reads more like:

If the line is completecompleted, append a line-break.

Also, thisThis test should be sufficient and is maybe easier to read:

Now, the condition is still hard to read. Let's simplify it to this:

This is now solooks quite simple, that you could easily already. You can alternatively use the ternary operator and rely on JavaScript casting the result to boolean:

This will be the result:The final result

Simply separate the two statements and it will work.

Personally, I would move this part to the end of the loop, as it reads like:

If the line is complete append a line-break.

Also, this test should be sufficient and is maybe easier to read:

Now, the condition is still hard to read. Let's simplify it to this:

This is now so simple, that you could easily use the ternary operator and rely on JavaScript casting the result to boolean:

This will be the result:

Remove the first else branch it will work.

Personally, I would move this part to the end of the loop, as it reads more like:

If the line is completed, append a line-break.

This test should be sufficient and is maybe easier to read:

Now, the condition is still hard to read. Let's simplify it:

This looks quite simple already. You can alternatively use the ternary operator and rely on JavaScript casting the result to boolean:

The final result

edited body
Source Link

You may haven't noticed it, because it's hard to spot using a whitespace. This happens, because you use an else if when appendingafter prepending the newline:

if (i !== 0 && j === 0) {} else if {} else if {}

Personally, I would move this part to the end of the loop, as it reads like:

var size = 8,
    result = '';"";

for (var i = 0; i < size; i++) {
    for (var j = 0; j < size; j++) {
        result += (i + j) % 2 ? "◼""◻" : "◻";"◼";

        if (size - 1 == j) {
            result += "\n";
        }
    }
}

console.log(result);

You may haven't noticed it, because it's hard to spot using a whitespace. This happens, because you use an else if when appending the newline:

if (i !== 0 && j === 0) {} else if {}

Personally I would move this part to the end of the loop, as it reads like:

var size = 8,
    result = '';

for (var i = 0; i < size; i++) {
    for (var j = 0; j < size; j++) {
        result += (i + j) % 2 ? "◼" : "◻";

        if (size - 1 == j) {
            result += "\n";
        }
    }
}

console.log(result);

You may haven't noticed it, because it's hard to spot using a whitespace. This happens, because you use an else if after prepending the newline:

if (i !== 0 && j === 0) {} else if {} else if {}

Personally, I would move this part to the end of the loop, as it reads like:

var size = 8,
    result = "";

for (var i = 0; i < size; i++) {
    for (var j = 0; j < size; j++) {
        result += (i + j) % 2 ? "◻" : "◼";

        if (size - 1 == j) {
            result += "\n";
        }
    }
}

console.log(result);
Source Link
Loading