Chapter 2 Exercises

Triangle:

let triangle = "";
for(i = 0; i < 8; i++) {
  triangle = triangle + "#";
  console.log(triangle);
}

FizzBuzz:

for (i = 1; i < 101; i++) {
    if (i % 3 === 0) {
        console.log("Fizz");
        if (i % 3 === 0 && i % 5 === 0) {
            console.log("BuzzFizz");
        }
        continue;
    } else if (i % 5 === 0 && i % 3 !== 0) {
        console.log("Buzz");
        if (i % 3 === 0 && i % 5 === 0) {
            console.log("BuzzFizz");
        }
        continue;
    }
    console.log(i);
}

Chessboard:

let chessBoard = "";
let size = prompt("Enter desired size:");

for (row = 0; row < size; row++) {
    for (column = 0; column < size; column++) {
        if ((row + column) % 2 === 0) {
            chessBoard += " ";
        } else {
            chessBoard += "#";
        }
    }
    chessBoard += "\n";
}

console.log(chessBoard);
1 Like

LOOPING A TRIANGLE

  for (x=0;  x< 7; x++) {
  a = "#"
    for (y=0; y < x ; y++) {
      a += "#";
    }
 console.log(a)
}

FIZZBUZZ

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

  var multiplier3 = i%3
  var multiplier5 = i%5

      if(multiplier3===0 && multiplier5===0){console.log("FizzBuzz")
      continue;
      }
      if (multiplier3===0){console.log("Fizz");
      }
      if(multiplier5===0 && multiplier3!=0){console.log("Buzz")
      }
      if(multiplier5!=0 && multiplier3!=0){console.log(i)
      }
}

CHESSBOARD

let size = 8;

let chessboard = "";

for (let i = 0; i < size; i++) {
  for (let x = 0; x < size; x++) {
    if ((x + i) % 2 == 0) {
      chessboard  += " ";
    } else {
      chessboard += "#";
    }
  }
  chessboard  += "\n";
}

console.log(chessboard );
1 Like

My solutions to these exercises :slight_smile: I found these exercises quite difficult, it took me a good few days before I was able to get there… I’m sure it will become easier as we go along!

// EXERCISE 1 - LOOPING TRIANGLE

let number = "#"
while (number <= "#######"){
  console.log(number);
  number = number + "#"

}

// EXERCISE 2 FIZZ BUZZ 1

var num = 0;

while(num<=99){
num = num +1;

if(num % 3 == 0)
console.log(“Fizz”);
else if (num % 5 == 0)
console.log(“Buzz”);
else {
console.log(num)
}
}

// EXERCISE 2 FIZZ BUZZ 2

var num = 0;

while(num<=99){
num = num +1;

if(num % 3 == 0 && num % 5 ==0)
console.log(“FizzBuzz”);
else if (num % 3 == 0 && num % 5 !=0)
console.log(“Fizz”)
else if (num % 5 == 0 && num % 3 != 0)
console.log(“Buzz”);

else {
console.log(num)
}
}

// EXERCISE 3 - CHESSBOARD

var size = 8;

var toPrint = “”;

for (var row = 0; row < size; row++) {
for (var col = 0; col < size;col++) {
if ((col + row) % 2 == 0) {
toPrint += " ";
} else {
toPrint += “#”;
}
}
toPrint += “\n”;
}

console.log(toPrint)

1 Like

I spent three plus weeks while I was sick. I spent two weeks going over JS in other sites and docs but ultimately I cheated. I solved some of it on my own but eventually combined several solutions and tried to make them my own.

FizzBuzz:

Puzzle:

credits: CodePen Coding Site
JS Tutorial and others

1 Like

Same here - it took me even longer but I think some people here have more experience and I think at first is difficult and gets easier, relatively speaking as we move forward.

1 Like

2.1 Looping a Triangle

x = "#";

for(let i = 0; i < 8; i++){
 console.log(x);
 x = x + "#";
}

2.2 FizzBuzz

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

2.3 Chessboard

I didn’t get very far on this one before looking at the answer.

This is as far as I got:

var even = 0;

for(let i = 0; i < 8; i++){
   if(even % 2 == 0){
     console.log(" " + "#");
     }
   else{
     console.log("#" + " ");
      }
      even ++;
    }

One idea I had was to assign a string to var spaceHash = " " + “#”
and then add spaceHash + spaceHash by the desired number of rows/columns using a loop on odd lines.
Then doing the opposite for var hashSpace = “#” + " " for even lines.
However, the provided solution is much, much better.

I sure hope so! Good luck :blush:

Hi everybody.
To Ivan’s solution- really good. I used this one instead with only one loop:
endNum = 7; // We have seven rows
hash = “” ; // We need this empty
for (var aaa = 0, aaa<endNum; aaa++){
console.log(hash = hash + “#”) ;// first iteration will be hash = hast ("") + “#” => hast = “*”;
}
My question is to Ivan’s solution:
numberOfRows = 7;
for (var row = 0; row < numberOfRows; row++) { // here still row = 0;
toPrint = “#” ;
for (column = 0; column < row; column++);{ // first time this for loop will be not executed because row still equals = 0
toPrint =+ “#”;}
console.log(toPrint);
}

Hi @D3N1X,

A little modification to your code is needed. There were a few syntactical errors.
I have pasted the modified code below.

endNum = 7; // We have seven rows
hash = '' ; // We need this empty
for (var aaa = 0; aaa< endNum; aaa++){
console.log(hash = hash + '#') ;// first iteration will be hash = hast ("") + “#” => hast = “*”;
}
numberOfRows = 7;
for (var row = 0; row < numberOfRows; row++) { // here still row = 0;
toPrint = '#' ;
for (column = 0; column < row; column++){ // first time this for loop will be not executed because row still equals = 0
toPrint =+ '#';}
console.log(toPrint);
}

Happy Learning! :smiley:

2 Likes

Thank you, Malik. I really appreciate it.
I see it. Just I used to use " " instead of ‘’, I hope is not a mistake.

Hi,

  1. There is a comma in your for loop parameters. It should have semi colon instead.

Fixed it here.

  1. You can see below, there is a semi colon before the body opening brackets which is incorrect.

I have removed it here.

Hope this clears it out.

2 Likes

Yes, it is. Thank you

Wow! So many ways to skin a cat! This is what I came up with for the Chessboard problem. Not very eloquent but it works:

var size = prompt(“What size chessboard would you like (enter the number of squares for a single side)?”)
var row1 = " #";
var row2 = “# “;
for(z = 0; z < (size / 2); z++){
for(x = 0; x < (size / 2); x++){
document.write(row1);
}
document.write(”
”);
for(y = 0; y < (size / 2); y++){
document.write(row2);
}
document.write("
");
}

1 Like

First the first triangle exercise, you actually don’t need two loops to do it. Here’s a simpler way:

var stars = "";
  i = 0;

  while (i < 7) {
    i++
    stars += "#";
    console.log(stars);
    //console.log(stars);
  }
1 Like

Here’s the fizzbuzz script:

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

    if (i % 3 == 0 && i % 5 != 0) {
      console.log("Fizz");
    } else if (i % 5 == 0 && i % 3 != 0) {
      console.log("Buzz");
    } else if (i % 5 == 0 && i % 3 == 0) {
      console.log("FizzBuzz");
    } else {
      console.log(i);
    }
  }
2 Likes

Here’s my final chessboard script:

  var i;
  var size = prompt("The board will be (x by x) in size. What number should x be?");

  for (i = 1; i <= size; i++) {
    var x = 0;
    var pattern;
    var line = "";

    for (x = 1; x <= size; x++) {
        if ((i % 2 != 0 && x % 2 != 0) || (i % 2 == 0 && x % 2 == 0)) {
            pattern = " ";
        } else if ((i % 2 != 0 && x % 2 == 0) || (i % 2 == 0 && x % 2 != 0)) {
            pattern = "#";
        }
        line += pattern;
    }
    console.log(line + "\n");
  }
1 Like

I used a couple different thinking styles to solve these exercises. The chessboard I made really code efficient (short) but that isn’t always the best way to code. Sometimes it impacts the readability of the code. Triangle and FizzBuzz could be made much shorter using similar thinking.

Triangle Loop

FizzBuzz

Chessboard

1 Like

LOOPING A TRIANGLE

FIZZBUZZ

CHESSBOARD

1 Like

loop a triangle

image
::::::::::::::::::::::::::::::::::::

2. FizzBuzz


:::::::::::::::::::::::::
3. chessboard

1 Like

These are my answers to the 3 exercises @ the end of chapter 2. :slight_smile:

Looping the Triangle

FizzBuzz

ChessBoard

1 Like