Hey, I also struggle a little to work through the ChessBoard, what caught me off was that we had to be able to make is any size. I first had it down in a moment but then had to redo the entire code to meet this request. The examples you provide are static and wont be able requesting the computer to “build” the board, instead you are doing the work for the computer. There are much better examples than mine, but they are not transparent so it’s not easy to see how the computer creates the lines.
Help with Chessboard, almost got it
Hello guys, this is my chessboard code, to the eye on a webpage it almost seems fine but when I compare it to someone else’s I find I have too many spaces in places I shouldn’t(ie start of line). Can someone explain what is wrong with it and how to correct ? I’d prefer HTML over java console.
Thanks !
var grid = 8;
var toPrint="#";
for (var row = 0; row < grid; row++) {
if (row % 2 == 0){
document.write(" ");
}
for(var col = 0; col < grid; col++){
if (col % 2 == 0){
document.write(" ");
}
if (col % 2 ==0){
document.write(toPrint);
}
}
document.write("<br>");
}
Hey, well I took a quick look at your code, I substituted the   with a defined " " space and I got this below. So there is something going on with the  . Now you need to flip it around so that you can create a checkboard. I used a boolean statement to flip them.
Triangle Loop
Great WebsiteTriangle
for (i = 1; i < 8; i++) {
console.log("#".repeat(i))
}
FizzBuzz
for (i = 1; i < 101; i++) {
if ((i % 3) === 0 && (i % 5) === 0) {
console.log(“FizzBuzz”)
}
else if ((i % 3) === 0) {
console.log(“Fizz”);
}
else if ((i % 5) === 0) {
console.log(“Buzz”);
}
else {
console.log(i);
}
}
Chessboard
var board = 8
for (i = 0; i < board/2; i++) {
if(i & 1) {
//when i is odd
console.log(" #".repeat(board/2));
}
else {
//when i is even
console.log("# ".repeat(board/2));
}
}
Triangle:
var toPrint = “#”;
for(var count = 0; count <= 7; count++) {
console.log(toPrint);
toPrint += “#”;
}
Fizzbuzz:
for(var count = 1; count <= 100; count++) {
if(count % 3 == 0 && count %5 == 0) {
console.log(“FizzBuzz”);
} else if (count % 3 == 0) {
console.log(“Fizz”);
} else if (count % 5 == 0) {
console.log(“Buzz”);
} else {
console.log(count);
}
}
Chessboard:
var board = 8
for (i = 0; i < board; i++) {
if(i % 2 == 1) {
console.log(" #".repeat(board/2));
}
else {
console.log("# ".repeat(board/2));
}
}
Looping a triangle
var rowString = "#";
for (var i = 0; i < 7; i++) {
console.log(rowString);
rowString+="#";
}
FizzBuzz
for (var i = 1; i < 101; i++) {
if ( (i % 3) == 0 ) {
if ( (i % 5) == 0 ) {
console.log("FizzBuzz");
}
else {
console.log("Fizz");
}
}
else {
if ( (i % 5) == 0 ) {
console.log("Buzz");
}
else {
console.log(i);
}
}
}
Chessboard
var rowOne = "";
var rowTwo = "";
for (var n = 0; n < size; n++) {
if ( n % 2 == 0 ) {
rowOne += " ";
rowTwo += "#";
}
else {
rowOne += "#";
rowTwo += " ";
}
}
for (var i = 0; i < size; i++) {
if (i % 2 == 0) {
console.log(rowOne);
}
else {
console.log(rowTwo);
}
}
-
Looping a Triangle:
var num_rows = 7;
for(var row = 0; row < num_rows; row++){ var toPrint = "#"; for(var column = 0; column < row; column++){ toPrint += "#"; } console.log(toPrint) }
-
FizzBuzz
for(var counter = 0; counter<100; counter=counter+1){
if(counter % 3 == 0){console.log(“Fizz”);}
else{console.log(counter)};
if(counter % 5 == 0){console.log(“Buzz”);}
else{console.log(counter)};
if(counter % 5 == 0 && 3 == 0){console.log(“FizzBuzz”);}
else{console.log(counter)};
} -
Chessboard
var num_rows = 8;
for(var row = 0; row < num_rows; row++){ if(row % 2 == 0){console.log(" # # # #");} else(console.log("# # # #")); }
-
"This is the FizzBuzz assignment."
//exercise code written here, for the ivanOnTechProgrammingSquad
for (var hodl = 1; hodl < 101; hodl++) {
if ((hodl % 3 == 0) && (hodl % 5 == 0)) {
document.write("
FizzBuzz
");}
else if (hodl % 3 == 0) {
document.write("
Fizz
“);}
else if (hodl % 5 == 0) {
document.write(“
Buzz
“);}
else {
document.write(”
”+ hodl +”
");}
}
</script>
or:
for (var hodl = 0; hodl <= 100; hodl++) {
if ((hodl % 3 == 0) && (hodl % 5 == 0)) {
console.log("FizzBuzz ");
}
else if (hodl % 3 == 0) {
console.log(“Fizz”);
}
else if (hodl % 5 == 0) {
console.log(“Buzz”);
}
else {
console.log(hodl);
}
}
Chessboard Assignment
Looping Triangle
var overall_rows = 7;
for(var row = 0; row<overall_rows; row++) {
var toPrint = "#";
for (var column = 0; column < row; column++) {
toPrint += "#";
}
console.log(toPrint);
}
Fizz Buzz
for (let n = 1; n <= 100; n++) {
let output = "";
if (n % 3 == 0) output += "Fizz";
if (n % 5 == 0) output += "Buzz";
if (output == "") {
console.log(n)
}
else {
console.log(output);
}
}
Chess Board
var size = 8;
var overall_rows = size;
for (var row = 1; row <= overall_rows; row++) {
var space = "";
if (row % 2 != 0) {
for (var column = 1; column <= size; ) {
space += " ";
column++;
space += "#";
column++;
}
}
else{
for (var column = 1; column <= size; ) {
space += "#";
column++;
space += " ";
column++;
}
}
console.log(space);
}
Triangle
I saw the book solution and thought it was cute. Obviously I can’t just post that here as my own work so I made a recursive version, despite what the book says about recursion overhead.
GitHub Gist
FizzBuzz
This one I’ve seen a few times in C, C++, Pascal, Fortran, and whatnot. I’m bored with the problem so I just brute-forced it, no attempt at elegance. There’s going to be a dangling condition, no matter how you try to slice the decision tree.
GitHub Gist
Chessboard
The elegant twist in the middle of this one is the use of (row + col) % 2 == 0 for switching colors. Otherwise it’s brute nested for-loop traversal of a 2D array. In Matlab this would have been a one-line dynamic indexing demo.
GitHub Gist
My solutions were hard to resolve but here they are. I left my notes in the exercise for posterity.
- First Hash Grid
</head>
<script>
// Javascript comments are coded differently than HTML comments
// JS comments begin with “//”
// All Javascript must be coded inside the script tag.
// NO HTML TAGS are allowed inside the script tag
// First example of a FOR loop prints a 7 line array of increasing hashes
/* Looks like this:
#######
*/
for (let answer = "#"; answer.length < 8; answer += "#")
console.log(answer);
</script>
<body>
2. FizzBuzz - This one was really fun. I ended up peeking at the answer.
FizzBuzz Website
<script>
/* This routine outputs numbers 1 to 100 and replaces numbers divisible by 3 with"Fizz"
and numbers divisible by 5 with “Buzz” and numbers divisible by BOTH with “FizzBuzz”
*/
for (let n = 1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;
document.write(output || n);document.write("
");
console.log(output || n);
}
</script>
<body>
<h1>This is the Fizzbuzz exercise</h1>
3. Checkerboard - I am really going to need to review this.
Hashtag Checkerboard
Hashtag Checkerboard
Looping a Triangle
var loopsize = 7;
for(var count = 0; count < loopsize; count++){
var text = "#";
for(var count1 = 0; count1<count; count1++){
text = text + "#";
}
document.write(text + "<br />");
}
FizzBuzz
for(var fizzcount = 1; fizzcount <=100; fizzcount++){
if(fizzcount % 5 == 0 && fizzcount % 3 == 0) {
document.write("FizzBuzz <br />");
}
else if (fizzcount % 5 == 0) {
document.write("Buzz <br />");
}
else if (fizzcount % 3 == 0) {
document.write("Fizz <br />");
}
else{
document.write(fizzcount + "<br />");
}
}
Chessboard
var number = prompt("Enter Grid Size");
var isodd = true;
for(var rowCount = 0; rowCount < number; rowCount++){
for(var columnCount = 0; columnCount < number; columnCount++){
if(isodd == false) {
document.write("-");
isodd = true;
}
else {
document.write("#");
isodd = false;
}
}
document.write("<br />");
isodd = !isodd;
}
Looping a triangle
let output = '';
for (let i=0; i < 7; i++) {
output = output + '#';
console.log(output);
}
Fizzbuzz
for (let i=0; i < 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {
console.log("FizzBuzz");
}
else if (i % 3 == 0) {
console.log("Fizz");
} else if (i % 5 == 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
Chessboard
const girdDimension = 8;
let evenGridRow = '';
let oddGridRow = '';
for (let i =0; i < girdDimension; i++) {
if (i % 2 == 0) {
evenGridRow = evenGridRow + ' ';
oddGridRow = oddGridRow + '#';
} else {
evenGridRow = evenGridRow + '#';
oddGridRow = oddGridRow + ' ';
}
}
evenGridRow = evenGridRow + '\n';
oddGridRow = oddGridRow + '\n';
for (let i =0; i < girdDimension; i++) {
if (i % 2 == 0) {
console.log(evenGridRow);
} else {
console.log(oddGridRow);
}
}
Triangle Loop
var num_rows = 7;
for(var row = 0; row < num_rows; row++){
var toPrint = “#”;
for(var column =0; column<row; column++){
toPrint += “#”;
}
document.write(toPrint + “
”);
}
Fizz Buzz
for(var i = 1; i <= 100; i ++){
var output = "";
if( i % 3 == 0) { output += "Fizz"; }
if( i % 5 == 0) { output += "Buzz"; }
if(output == "") { output = i;
}
console.log(output);
}
Chess Board
var size = 8;
var board = “”;
for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
if ((x + y) % 2 == 0)
board += " ";
else
board += “#”;
}
board += “
”;
}
document.write(board);
1.Triangle Loop:
toPrintb=""
for (var rowb =0;rowb < 7;rowb++) {
toPrintb+="#"
console.log (toPrintb);
}
2.FizzBuzz
var y=“Fizz”;
var z=“Buzz”;
var yz= “Fizzbuzz”;
var counter = 0;
var x = 0;
while (counter <= 100)
{console.log (x);
counter++
if (counter%3==0)
{x = y;
}else if (counter%5==0){x=z;
}else
{x=counter
}
if(counter%5==0 && counter%3==0)
{x=yz
}
}
3.Chess Board
var ra="";
var rb="";
var rc="";
size=8
for (var cc= 0;cc< size; cc++)
{ ra+="# " ;
rb+=" #" ;}
for( var concc=1;concc<=size; concc++)
{ if (concc%2!=0)
{rc=(rb); }
else {rc=ra}
console.log(rc);
}
- Exercise 1: Triangles
// Version 1 : "naive" version with 2 variables
let nbHashTag = 0;
let string = "";
while (nbHashTag < 7)
{
string += '#';
console.log(string);
nbHashTag += 1;
}
// Version 2 : "elegant" version with a property
let string = "";
while (string.length < 7)
{
string += '#';
console.log(string);
}
- Exercise 2: FizzBuzz
for (let i = 0; i < 100; i++)
{
if (i % 3 == 0 && i % 5 != 0)
{
console.log('Fizz because i = ' + i + ' so i % 3 == 0 AND i % 5 != 0');
}
else if (i % 3 != 0 && i % 5 == 0)
{
console.log('Buzz because i = ' + i + ' so i % 5 == 0 AND i % 3 != 0');
}
else if (i % 15 == 0)
{
console.log('FizzBuzz because i = ' + i + ' so i % 15 == 0');
}
}
- Exercise 3: Chessboard
// Version 1 : for the chessBoard
let size = 8;
for (let x = 0; x < size; x++)
{
let string = "";
for (let y = 0; y < size; y++)
{
if ((x + y) % 2 == 0)
{
string += " ";
}
else if ((x + y) % 2 == 1)
{
string += "#";
}
}
console.log(string + "\n");
}
// Version 2 : for any grid
let length = 6;
let width = 12;
for (let x = 0; x < length; x++)
{
let string = "";
for (let y = 0; y < width; y++)
{
if ((x + y) % 2 == 0)
{
string += " ";
}
else if ((x + y) % 2 == 1)
{
string += "#";
}
}
console.log(string + "\n");
}
Exercises Chapter 2
- Looping a Triangle
- FizzBuzz
- Chessboard
counter = 1;
hashtag = "#";
while (counter < 8) {
console.log(hashtag)
hashtag += "#"
counter += 1
}
let number = 0;
while(number < 101){
number +=1;
if (number % 3 == 0 && number % 5 == 0) {
console.log("FIZZBUSS");
}
else if (number % 3 == 0) {
console.log("Fuzz");
}
else if (number % 5 == 0 &! number % 3 == 0){
console.log("Buzz");
}
else {
console.log(number);
}
}
var size = 4;
var odd = "";
var even = "";
for(column = 0; column < size; column++){
odd += "# ";
even += " #";
}
for(row = 0; row < size; row++){
console.log(odd);
console.log(even);
}
// Looping a triangle
var num_rows = 8
for(var row=0; row < num_rows; row++) {
var toPrint = “#”;
for(var colum = 0; colum<row; colum++){
toPrint += “#”;
}
console.log(toPrint);
}
// FIZZ & Buzz
for (let n = 1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;
console.log(output || n);
}
// CHESSBOARD
let size = 8;
let board = “”;
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if ((x + y) % 2 == 0) {
board += " ";
} else {
board += “#”;
}
}
board += “\n”;
}
console.log(board);
- Looping a Triangle
counter = 1;
hashtag = “#”;
while (counter < 8) {
console.log(hashtag)
hashtag += “#”
counter += 1
}
2. FizzBuzz
for(var count=1;count <= 100;count++)
{
if(count % 3 ==0)
{console.log(“Fizz”);
}else if(count % 5 ==0){
console.log(“Buzz”);
}else{
console.log(count);}}
3.Chessboard
var board=8
for(var n=1;n<=board;n++){
if(n % 2==1){
console.log("# “.repeat(board/2));
}
else{
console.log(”# ".repeat(board/2));
}
}