Chapter 2 Exercises

Please show me how to set up the Chessboard exercise. Thank you!

Hi @jgentry,

Please find other student’s answers for inspiration. There are many great answers! :smiley: And also you can find my explanation below for how these loops work for the chessboard exercise.

Hope this helps.

2 Likes

Okay. I’ve been referring to other students’ answers to get help. But in some ways those answers are structured, the console won’t accept them. Anyway, thanks!

I still don’t understand. How am I supposed to set the problem up? The clip and the instructions you gave me barely helped me.

I did my best. I know it does not look like what is described in the book, but it is the best I could do. I just gave up and looked at the correct answer and tried to follow it. I was tired of being stuck on these exercises.

for(var row=0; row<8; row++){
    for(let col = 0; col < row; col++){
        if ((row + col) % 2 == 0){
            board += " ";}
        else{board += "#";
            }
    }
board += "\n";
}
console.log(board)

All the time I’ve been enrolled in the Academy I’ve quickly moved through the courses without even bothering to practice what I’ve learned. Now I’ve been required to slow down and try to put what I learn into practice. That’s not so easy.

Some things are easier said than done. That is especially true for these exercises.

Thank you!

Hi,
I’ve added some comments. Try to relate it with the previous post I linked. Hope this will help too.

var row = 8; // Define number of rows 
var col = 8; // Define number of columns

// Loop over each row
for (var rowNumber = 1; rowNumber < row+1; rowNumber++ ) {
  // Define the string
  var chessboard = “”;
 // Loop over each column for that particular row number
  for (var columnNumber = 1; columnNumber < col+1; columnNumber++) {
     //If you add column number and row number , if the sum is Even then print "#"
     if ((rowNumber + columnNumber)%2==0) {
       chessboard += “#”
     }
   //If you add column number and row number , if the sum is Odd then print " " (empty string)
    else if ((rowNumber+columnNumber)%2 !== 0) {
      chessboard += " "
     }
   }
// Print the chessboard 
console.log(chessboard);
}
1 Like

I’ll go back in and do the exercise again. It was causing me some complications. Thank you!

When I try to put this into my console,

var row = 8;
var col = 8;

it gives me undefined when I press ENTER. Can you please help me?

Don’t worry about any of the posts I have written recently. Your solution actually helped me in solving the Chessboard exercise. Thank you!!!

I suppose I had to do this one in Atom rather than in the console of Google Chrome.

var row = 8;
var col = 8;
for(var rowNumber = 1; rowNumber < row+1; rowNumber++ ){
  var chessboard = "";
  for(var columnNumber = 1; columnNumber < col+1; columnNumber++ ){
    if ((rowNumber+columnNumber)%2==0) {
      chessboard += "#"
    }
    else if ((rowNumber+columnNumber)%2!==0){
      chessboard += " "
    }
  }
  console.log(chessboard);
}
1 Like

Triangle
for (var result = “#”; result.length <=7; result = result + “#”)
console.log(result);

Fizzbuzz
for(number=0;++number<101;console.log(number%5?x||number:x+‘Buzz’))x=number%3?’’:‘Fizz’

Chessboard

var chessBoard= ‘’,

size= 8,

c;

for ( var i = 0 ; i < size ; i++) {

c= i%2 ? ‘# ’ : ’ #’;

for ( var j = 0 ; j < size/2 ; j++) {

chessBoard+= c;

}

chessBoard+= ‘\n’;

}

console.log(chessBoard);

@jgentry, Fantastic ! :smiley:
Console is usually used for short functions or statements to play around with. When you want to write relatively big or complex code, it is always recommended to use IDE’s such as Atom or VS Code.

Happy Learning ! :slight_smile:

1 Like

Hi @mickymax777, Hope you’re having a wonderful day. I would request you to please paste your code in the desired format so that we can evaluate your code and give feedback.

Please take a look at the picture below.

Regards

1 Like

I used the console to basically practice simple functions like the ones I described earlier. When it comes time to write longer, more complex functions, I will need to use Atom. The console will not be so helpful for those. I’ll learn more about those soon enough.

Thanks for the comment!

Triangle
for (var result = “#”; result.length <=7; result = result + “#”)
console.log(result);

Fizzbuzz
for(number=0;++number<101;console.log(number%5?x||number:x+‘Buzz’))x=number%3?’’:‘Fizz’

Chessboard

var chessBoard= ‘’,

size= 8,

c;

for ( var i = 0 ; i < size ; i++) {

c= i%2 ? ‘# ’ : ’ #’;

for ( var j = 0 ; j < size/2 ; j++) {

chessBoard+= c;

}

chessBoard+= ‘\n’;

}

console.log(chessBoard);
1 Like

FIZZ BUZZ

var a = “Fizz”;
var b = “Buzz”;

for(var counter = 1; counter < 101; counter++){
    if (counter % 15 == 0){console.log(a + " " + (a+b))}
    else if(counter % 3 == 0){console.log(a)}
    else if(counter % 5 == 0){console.log(b)}
    else {console.log(counter);}

}

1 Like

CHESS BOARD (not quite)

var num_rows = 8;
var num_columns = 8;

for(var row = 0; row < num_rows; row++) {
for(var column = 0; column < num_columns; column++)
if((row + column) % 2 == 0)
console.log(" “);
else{console.log(”#"); }
}

CHESSBOARDS Input Custom Square Grid

var size = prompt(“Grid Size”)
var num_rows = size;
var num_columns = size;

for(var row = 0; row < num_rows; row++){
var chessboard = “” ;
for(var column = 0; column < num_columns; column++){
if((row+column) % 2 !== 0) {
chessboard += “#”
}
else if((row+column) % 2 == 0){
chessboard += " "
}
}
console.log(chessboard);
}

1 Like

Triangle shape code:
for(let i=0;i<7;i++){
document.write("
");
for(let j=0;j<=i;j++){
document.write("*");
}}

Fizz Buzz Code:
for(let i = 1; i <= 100; i ++) {
if((i % 5 === 0) && (i % 3 !== 0)){
document.write(“Buzz”);}
else if(i % 3 === 0) {
document.write(“Fizz”);
}
else {
document.write(i);
}
document.write("
")
}

3. Board Code:
let binding = 8;
let toPrint = “”;
for(let i = 0; i < binding; i ++){
if((i === 0) || (i % 2 === 0)){
toPrint = " ";
for(let j = 1; j < binding; j ++){
if(j % 2 !== 0){
toPrint += “#”;
} else {
toPrint += " ";}}
//console.log(toPrint0);
}

  else if ((i !== 0) || (i % 2 !== 0)) {
    toPrint = "#";
    for(let k = 1; k < binding; k ++){
      if(k % 2 === 0){
        toPrint += "#";
      } else {
        toPrint += " ";
    } //console.log(toPrint1); }
  }} console.log(toPrint);}
1 Like

Having the most trouble wrapping my head around the last Chess Board exercise. I think its the double loop and modulus Im most hung up on. Why the use of modulus?

Hi @chommp,
Modulus is used to return the remainder of the division between those two numbers.

Happy Learning! :slight_smile:

2 Likes