Quote:
Originally Posted by gaming_mouse
Quote:
Originally Posted by Somnius
getting the impression the timing of my post was less than optimal
The code is pretty verbose. Quick rewite:
Code:
console.log( makeBoard(8,8) );
function makeBoard(w,h) {
var row = repeatStr(' #', w) + "\n";
return repeatStr(row, h);
}
function repeatStr(str, n) {
return new Array(n+1).join(str);
}
Not a JS dev, but does that do the same?
If I understand the initial block correctly, the hash symbols are supposed to alternate by row, much like a chess board pattern if I'm not wrong.
@Somnius: either way, try to reduce as much repetition of code as necessary. That's a good step to begin with.
In your example, and without giving away solutions, in every if/else block the code is identical except for one or two lines.
Start by extracting all the identical lines to the outside of the blocks (start with one innermost block to get a feel for it), then when you end up with very few lines, see if you cannot reduce that part, too.
As an example (assuming all variables have been defined & initialised prior):
Code:
if (onething == 0) {
this += 1;
that += 2;
other += fluffy;
} else {
this += 1;
that += 2;
other += fluffy - 1;
}
turns into:
Code:
this += 1;
that += 2;
if (onething == 0) {
other += fluffy;
} else {
other += fluffy - 1;
}
turns into:
Code:
this += 1;
that += 2;
other += (onething == 0) ? fluffy : fluffy - 1;
etc... (hint: fluffy is still being repeated, could be extracted, too)