One of the exercises in "Eloquent Javascript" called for writing code for creating a chessboard pattern of hashtags and spaces for a square shape in the console.
Here is my code (it seems to work)
var number = 13;
var computerN = number * number + number - 1;
var string = ' ';
while (string.length < computerN)
{
if (number % 2 === 0)
{
string = string + '#';
if (string.length === computerN)
{
break;
}
else if ((string.length - number) % (number + 1) === 0)
{
string = string + '\n#';
}
string = string + ' ';
if (string.length === computerN)
{
break;
}
else if ((string.length - number) % (number + 1) === 0)
{
string = string + '\n ';
}
}
else
{
if (string.length === computerN)
{
break;
}
else if ((string.length - number) % (number + 1) === 0)
{
string = string + '\n# ';
}
string = string + '#';
if (string.length === computerN)
{
break;
}
else if ((string.length - number) % (number + 1) === 0)
{
string = string + '\n #';
}
string = string + ' ';
}
}
console.log(string);
No comments:
Post a Comment