Chapter 2 Exercises

Triangle Loop:

ChessBoard Loop:

var filasColumnas = 8; strChess1 = " #"; strChess2 = " #"; strChess3 = " #"; strChess4 = " #"; for (var l = 0; l < filasColumnas; l++) { strChess3 = strChess3 + strChess1; strChess4 = strChess4 + strChess2; } for (var m = 1; m <= filasColumnas; m++) { if (m % 2 == 0) { document.write("
"+ strChess3 +"
"); }else { document.write("
"+ strChess4 +"
"); } }
1 Like

I think this is as good as time to say I have no idea why putting the brackets “[ ]” at some places changes how the bracket positioning effects how the code looks after being executed so I tend to open it and stuff everything else in to finishing up the rest of the code. I’d like some help on that and also with my issue when printing out the chess board in the console log ( details to that below)

Triangle Loop
var num_row=7;
for(var row=0; row<num_row; row++)
{var toPrint="#";
for(var column=0; column < row; column++) toPrint+="#";
console.log(toPrint+"\xa0"+ “to”)}

FizzBuzz
for(num=1;num<=100; num++){
if(num%3==0&&num%5==0) console.log(“fizzbuzz”)
else if (num%3==0)console.log(“fizz”)
else if(num%5==0)console.log(“buzz”)
else console.log(num);}

ChessBoard

var chess=""
var size=8,c;
for (var x=0; x<size;x++) {c=x%2==0 ? " #":"# " ;
for (var y=0; y<size/2;y++)
chess+=c;
chess+= “\n”;
console.log(chess)}

I was reminded of the need to work from top down and learned about the remainder operator with the chess board exercise I learned about the conditional operator one issue though with printing this in the console log it gradually increased like the looping triangle did until it made the chessboard why did it not just print out the chessboard ?

2 Likes

Looping 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
var row = 100;
for(var num = 0; num < row; num++){
let num_rows = num+1
let toPrint = num+1
if(toPrint % 3 ==0 && toPrint % 5 == 0){
var result = “FizzBuzz”
} else if (toPrint % 3 == 0){
var result = “Fizz”
} else if(toPrint % 5 ==0){
var result = “Buzz”
} else result = toPrint
console.log("Row " + num_rows + " result is " + result)
}

Chessboard
var size = prompt(“Please enter chessboard size”);
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 += “\n”;}
console.log(board);

1 Like

FizzBuzz
I’m happy with how this finally turned out, but it can no doubt be condensed.

  for(a = 1; a <= 100; a++){
      if(( a % 3 == 0) && ( a % 5 == 0)){
        console.log("FizzBuzz"); continue;
      }
      if( a % 3 == 0){
        console.log("Fizz"); continue;
      }
      else if( a % 5 == 0){
      console.log("Buzz"); continue;
      }
      console.log(a);
      }

Chessboard
This one has been very difficult and I wasn’t able
to crack it on my own. Reverted to studying others’ work.

var size = 8;
      var board = " ";

      for(a = 0; a < size; a++){
       for(x = 0; x < size; x++){
         if((a + x) % 2 == 0){
           board += " ";
         }
         else {
           board += "#";
         }
           }
           board += "\n";
     } console.log(board)
1 Like

Looping a Triangle

	    let rows = 7;
        let message = "#";

        for(let i = 1; i <= rows; i++){
            document.write("<p>" + message +"</p>");
            message += "#";
        }

FizzBuzz

        let fizz = "Fizz";
        let buzz = "Buzz";
        let fizzbuzz = "FizzBuzz"

        for(let i = 1; i <= 100; i++){

            if(count % 3 == 0 && count % 5 == 0){
                document.write("<p>" + fizzbuzz + "</p>");
            }

            else if(count % 3 == 0){
                document.write("<p>" + fizz + "</p>");
            }

            else if(count % 5 == 0){
                document.write("<p>" + buzz + "</p>");
            }

            else {
                document.write("<p>" + i + "</p>");
            }
        }

Chessboard

        let board = "";
        let size = 8;

        for(let y = 0; y < size; y++){
            for(let x = 0; x < size; x++){
                if((x + y) % 2 == 0){
                    board += " ";
                }
                else {
                    board += "#";
                }
            }
            board += "\n"; // This creates a new line on the y axis,
        }                  // the loop on the x axis will begin again.
        console.log(board);
1 Like

If you don’t understand (trust me I didn’t when looking at the x and y variables), check this video out. It looks long but you only really need to watch ten minutes on from where it begins, for the solution explanation.

Eloquent Javascript - Chessboard Explained

You don’t need to skip, just play, watch, listen and rewind if needed. :+1:

1 Like

I could not figure these problems out on my own and needed to spend a lot of time with YouTube to make them happen, even with writing the code that was on the screen, I still made mistakes and had to walk away a few times then come back to them. I did learn how to do them, but feel Blahhh as I could not figure these out on my own. NGMI :wink:

Fizz Buzz:

for ( i = 1; i <= 100; i = i+1);
     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);

Chess Board:

// create a size variable set it equal to 8
let size = 8;
//create an empty string result
let result = "";

//Loop to create the rows
let row = 1;
while (row <= size){
    //loop to create the columns
    let column = 1;
    while (column <= size){
      //if column plus row is even
      if ((column + row) % 2 ==0){
        //add and empty space
        result += " ";
        //else
    } else {
        //Add #
        result +="#";
      }
      column +=1;
    }
    //add a new line symol to end current rows
    result += "\n";
    row += 1;
  }
//log the string to the console.
console.log(result);

The chess board code is super simple! (I had a really hard time with this as well)

Once you see it you say, I could have done it! I feel like we that are just starting out over think things and need to not think so hard. Solutions seem to be simple.

I wish that I could offer you some advice with the [ ]. But I have no idea as well. Reading through these solutions to see other ways of doing this. There is more than 1 way to skin the cat in JavaScript.

Triangle Loop:

for (let row = “#”; row.length < 8; row += “#”)
console.log(row);

Fizz Buzz:

for (let number = 1; number <= 100; number = number + 1)
{
let output = “”;
console.log(number);
if (number % 3 == 0) output += “Fizz”;
if (number % 5 == 0) output += “Buzz”;
console.log(output || number);
}

Chessboard:

let noofsquare = 8;

let square = “”;

for (let y = 0; y < noofsquare; y++) {
for (let x = 0; x < noofsquare; x++) {
if ((x + y) % 2 == 0) {
square += " ";
} else {
square += “#”;
}
}
square += “\n”;
}

console.log(square);

1 Like

Fizz buzz

Triangle

chess board

CHESS BOARD

function chess(size){
var board = “”;
for(var a = 0; a < size; a++){
for(var b = 0; b < size; b++){
board += a % 2 == b % 2 ? " " : “#”;
}
board += “\n”;
}
return board;
}
console.log(chess(12));

So yeah still struggling with the modulo spend an hour and a half on this and by far this was the hardest exercise. But yeah just gonna have to drill all these into my head so I understand them fluently and can use them more efficiently in the next exercise. I had assistance from my friend who is a developer already unfortunately but yh grind dont stop. Have a good evening guys!

1 Like

fizzbuzz

great website
    <script>

      // WRITE EXERCISE CODE
      for ( var a = 1; a < =100; a++){
        if ( a % 3 ===0 && a % 5 === 0){
          console.log ( a + " FizzBuzz ");}
          else if ( a % 3 === 0){
            console.log( a + " Fizz ");}
            else if ( a % 5 === 0){
              console.log( a + " Buzz ");}
              else { console.log(a);}
      }
    </script>

  </body>
chessboard great website
    <script>

      // WRITE EXERCISE CODE
      let size = 8;
      let board = " ";
      for (let a = 0; a < size; a++){
        for (let b = 0; b < size; b++){
          if((a + b) % 2 == 0){
            board += "  ";}
            else { board += " # ";}}
            board += " \n ";}
            console.log( board );

    </script>

  </body>
1 Like

1st row with one # is getting skipped, I guess there is a typo :slightly_smiling_face:

Triangle Pattern

var l = “#” ;
while ( l.length<8) {
      console.log(l);
      l+="#";
}

FizzBuzz

var n = 1;
while (n<=100) {
      var output ="";
      if (n%3==0) output+=“fizz”;
      if (n%5==0) output+=“buzz”;
      console.log(output||n);
      n++;
}

Chessboard

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

1 Like

I just miss the space between the ‘’#’’
In this case how can it be done?

var outputGrid = “”;
var nums = 64;

for (var num = 1; num <= nums ; num++)
{
if (num % 2)
{
//dispari
outputGrid += “#”;
}
else
{
//pari
outputGrid += " ";
}
if (num % 8 == 0)
{
//ogni 8 aggiunge ritorno a capo
outputGrid += ‘\n’;
}
}
console.log(outputGrid);

Triangle Loop

The triangle loop was already demonstrated by Ivan in a video, the code he presented was the following:
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);
}
It could be done in a shorter version by using the length of the “line”:
for(let line = “#”;line.length < 8; line += “#”)
console.log(line)

FizzBuzz

For FizzBuzz, this is what I came up with for myself, it did need a few tweeks to perfect the result:
// Your code here.
for(a=1;a<=100;a++){
if(a%3==0 && a%5!==0){console.log(“Fizz”);}
else if(a%5==0 && a%3!==0){console.log(“Buzz”);}
else if(a%5==0 && a%3==0){console.log(“FizzBuzz”);}
else console.log(a);}

Chessboard

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

1 Like

Triangle Loop

let hashtag - “#”;

while(hashtag.length < 7) {
console.log(hashtag);
hashtag = hashtag + “#”;
}

FizzBuzz Loop

// 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);

}

ChessBoard Loop

let spaceHashtag = " #";

let hashtagSpace = "# ";

for (let i = 0; i < 8; i++) {
if(i % 2 === 0)
console.log(spaceHashtag.repeat(4));
}else {
console.log(hashtagSpace.repeat(4));
}

}

1 Like

this will work

var outputGrid = "";
var nums = 64; 
var row = false;

for (var num = 1; num <= nums ; num++)
{
 if(row){
  if (num % 2 === 0)
{
  outputGrid +=" "; 
} 
else
{
outputGrid += "#";
} 
  
}else{
 if (num % 2 === 0)
{
  outputGrid +="#"; 
} 
else
{
outputGrid += " ";
}  
}

if (num % 8 === 0)
{ 
outputGrid += '\n';
if(row){
  row = false;
}else{
  row = true;
}
  
}
}
console.log(outputGrid);

But it is easier to use nested loop

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);
1 Like