Chapter 2 Exercises

**Triangle **

let TotalRow = 7;
var Row = 0;
var CharToPrint = "#";
while (Row <= TotalRow)
{
    console.log(CharToPrint+ "\n");
    CharToPrint += "#";
    Row = CharToPrint.length;
}

FizzBuzz

let n =0;
while (n < 100)
{
  n++;
  if (n % 5 == 0 && n % 3 == 0)
  {
    console.log("FizzzzzBuzzzz" + "\n");
    n++;
  }
  if (n % 3 == 0)
  {
    console.log("Fizz" + "\n");
    n++;
  }
  if (n % 5 == 0)
  {
    console.log("Buzz" + "\n");
    n++;
  }
  console.log(n + "\n");
}

ChessBoard

let BoardSize = 8;
let CellColor ="";
for (let Col = 0; Col < BoardSize; Col++)
{
 for (let Row = 0; Row <BoardSize; Row++)
 {
   if ((Col + Row) % 2 == 0)
    { CellColor += "□";}
   else {CellColor += "■";}
  }
   CellColor += "<br>";
 }
document.write(CellColor);
1 Like

Looked at the answers, seem like I am taking a long way.

FIZZ BUZZ
var i = 0;
while(i < 100){
if ((i % 3 == 0) && (i % 5 == 0)) {
console.log(“FUZZ-BUZZ”);
} else {
if ((i % 5) != 0){
if ((i % 3) != 0){
console.log(i);
}
else {
console.log(“FUZZ”);
}
}
else {
console.log(“BUZZ”);
}
}
i++;
}

CHESSBOARD
let row1 = “”;
let row2 = " ";
cantRow = Number(prompt(“Numer de Filas y Columnas? “));
for (var i = 0; i < cantRow; i++) {
row1 = row1 + " #”;
row2 = row2 + " #”;
}
for (var i = 0; i < cantRow; i++) {
if (i % 2 == 0) {
console.log(row2);
} else {
console.log(row1);
}
}

1 Like

1. Triangle

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

2. FizzBuzz

let num = 1
while (num <= 100){
if (num % 3 === 0 ){
console.log(“Fizz”);
continue}
if (num % 5 === 0) {
console.log(“Buzz”);
continue}
if ((num % 3 === 0) && (num % 5 === 0)){
console.log(“FizzBuzz”);
}
console.log(num)
num++;
}

3. Chess Board:

let size = 8;
let output = " ";

let row = 1;
while (row <= size){
let column = 1;
while (column <= size){
if ((row + column) % 2 === 0){
output += " "
} else {
output += “#”;
}
column++;
}
output += ‘\n’;
row++;
}
console.log(output);

1 Like

Excellent work. Looping a triangle was quite straight forward for me. I still cannot get the point with the || in the last line in the “FizzBuzz”. I do not understand it at all. Trying to wrap my head around it and I cannot. The Chess game is even more difficult for me to comprehend.
Do assist me please.
Thanks.

triangle loop
for (let line = “#”; line.length < 8; line += “#”) {
console.log(line);
}

fizzbuzz
for (var fizzBuzz = 1; fizzBuzz <= 100; fizzBuzz++) {
if (fizzBuzz % 3 === 0 && fizzBuzz % 5 === 0) {
console.log(“FizzBuzz”);
}
else if (fizzBuzz % 3 === 0) {
console.log(“Fizz”);
}
else if (fizzBuzz % 5 === 0) {
console.log(“Buzz”);
}
else {
console.log(fizzBuzz);
}
}

chessboard
let size = 8;

let 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

let rows = 6;
let print = “#”;
while (print.length <= rows)
{ console.log(print);
print += “#”; }

How does my chessboard differ from yours. This is because I can absolutely understand how Javascript interpreted your code but I cannot understand how it interpreted mine. See code below;
for (let size = 0; size < 8; size++){
let board = “”;
for (let count = 0; count < 8; count++){
if ((size + count) % 2 == 0){
board += “#”;
}
else {
board += " ";
}
}

}
console.log(board);

How does my chessboard code below differ from the provided solution to chessboard exercise. See code below;
for (let size = 0; size < 8; size++){
let board = “”;
for (let count = 0; count < 8; count++){
if ((size + count) % 2 == 0){
board += “#”;
}
else {
board += " ";
}
}

}
console.log(board);
Help Please!!!

1 Like

image

the answer to making that Morocco quiz does not work. I need help.

1 Like

Triangle JS
var stars = “”;
var counter = 0;
while (counter<7){
stars += “#”;
counter++;
console.log(stars);
}

FizzBuzz JS
var toPrint = “”;
for (var row =1; row <= 100; row++) {
if ( (row % 5 == 0) && (row % 3 == 0)){
toPrint += “FizzBuzz”;
} else if (row % 3 == 0){
toPrint += “Fizz”;
} else if (row % 5 == 0){
toPrint += “Buzz”;
} else {
toPrint += row;
}
}
console.log(toPrint);

ChessBoard JS
var size = parseInt(prompt(“Enter size”));
var toPrint = “”;
for (var row = 0; row <= size; row++) {
for (var col = 0; col <= size; col++) {
if ((col % 2) == 0){
toPrint += " ";
} else {
toPrint += “#”;
}
if ((row % 2) == 0 && col == 0){
toPrint += " "; }
}
toPrint += “\n”;
}
console.log(toPrint);

1 Like

Hey @Reggy2323, hope you are great.

you var y or correct_answer value must be an string, meaning it should be:

var y = "rabat"
// OR
var correct_answer = "rabat"

String values must be capture within "".

Carlos Z

Hey @Omas, hope you are great.

you had declared your board variable inside your for loop, mean it will stop once the iteration finish, you should take it out of the for loop.

Also, at the end of the first for loop, you should add board += "\n"which will give you a “new line” after each iteration of that for loop.

Here is the complete code:

let board = ""; //board variable outside for loop
for (let size = 0; size < 8; size++) {
  for (let count = 0; count < 8; count++) {
    if ((size + count) % 2 == 0) {
      board += "#";
    } else {
      board += " ";
    }
  }
  board += "\n"; //new line after iteration
}
console.log(board);

Carlos Z

//looping a triangle

var hashtag = " ";
var counter = 0;
while (counter < 8) {
hashtag = hashtag + "#";
console.log(hashtag);
counter++;
}

//fizzbuzz

for (let i = 1; 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


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

1 Like

TRIANGLE LOOP

var hash = "";
var counter = 0;
while(counter<7){
  hash += "#"
  document.write(hash + "<br>");
  counter++;
} 

FIZZBUZZ

var i;
var max = 100;
for (i = 1;i<=max;i+=1){
  if (i % 3 == 0 && i % 5 == 0){
    document.write("FizzBuzz" + "<br>");
  }
  else if(i % 3 == 0){
    document.write("Fizz" + "<br>");
  }
  else if (i % 5 == 0){
    document.write("Buzz" + "<br>");
  }
  else{
    console.log(i)
    document.write(i + "<br>");
  }
}

CHESSBOARD

var size = 8;
var string1 = "";
var string2 = "";
var outstring = "";
for(var i = 1; i <= size;i+=1){
  if (i % 2 != 0){
    string1 += "&nbsp;";
    string2 += "#";
  }
  else {
    string1 += "#";
    string2 += "&nbsp;";
  }
}
for(var i = 1; i <= size;i+=1){
  if (i % 2 != 0){
    outstring = outstring + string1 + "<br>";
  }
  else {
    outstring = outstring + string2 + "<br>";
  }
}
document.write(outstring)
1 Like

Chapter 2 Exercises

looping_a_triangle.js
var counter = 0;
for(counter; counter < 7; counter++){
  hash = hash + "#";
  console.log(hash);
}

fizzbuzz.js
var fizz = "Fizz";
var buzz = "Buzz";
for(xxx; xxx < 101; xxx++){
  if(xxx % 3 == 0){
    if(xxx % 5 == 0){
      console.log(fizz + buzz);
    }
    else{
      console.log(fizz);
    }
  }
  else if(xxx % 5 == 0){
    console.log(buzz);
  }
  else{
    console.log(xxx);
  }
}

chessboard.js
var blackSquare = "#";
var pairSquares = " ";
//You only need to change the integer "size" to change the gridsize!
var size = 8;
var row = 0;
var column = 0;
for(row; row < size; row++){
  for(column; column < size; column++){
    if(column % 2 == 0){
      pairSquares = whiteSquare + blackSquare;
      if(size % 2 == 0){
        console.log(pairSquares.repeat(~~(size/2)));
      }
      else if(size % 2 == 1){
        console.log(pairSquares.repeat(~~(size/2)) + whiteSquare);
      }
    }
    else{
      pairSquares = blackSquare + whiteSquare;
      if(size % 2 == 0){
        console.log(pairSquares.repeat(~~(size/2)));
      }
      else if(size % 2 == 1){
        console.log(pairSquares.repeat(~~(size/2)) + blackSquare);
      }
    }
  }
  "\n";
}

EDIT: Edited for formatting

1 Like

Hi guys,

Here’s my code for the exercises. Looking at the original solutions though - :scream_cat:

for (num=1;num<101;num++)
    {
        if (num%3==0 && num%5!=0) {document.write("Fizz, ");} 
        else if (num%5==0 && num%3!=0) {document.write("Buzz, ");}
        else if (num%5==0 && num%3==0) {document.write("FizzBuzz, ");}
        else document.write(num+", ");
        
        document.write("<br/>");
    }
    var output = "";
    for (num=0;num<8;num++)
    {
        if (num%2==0) {output="*&nbsp;"} 
        else {output="&nbsp;*"}
        
        for (col=0;col<8;col++) 
        {
            document.write(output);
            }
        document.write("<br/>");
    }
1 Like

for (let line = “#”; line.length < 8; line += “#”)
console.log(line);
function “fizzbuzz” --this is right answer
for (i=1; 1==< 100; 1++;)
for (1%3==“fizz”; 1%5==“buzz”);
else console.log (“fizzbuzz”);
this is what i worked out
function "fizzbuzz;
for (i=1; 1 ==<100; 1++;)
for (1%3== “fizz”; 1%5 ==“buz”);
else console.log ‘[]’
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);

could not get on my own

1 Like

ok, so the triangle loop we did a while back actually and I just extrapolated for that one. the FizzBuzz thing had me going for a while, then I finally realized how to write out if the row is divisible by 3 or 5 or 3 and 5 in code and that made everything clear. The chess board exercise I had no idea and had to look up the solution for. Anyway, here is how I solved FizzBuzz:

var num_rows = 101
for(var row = 0; row < num_rows; row++){
if (row % 3==0){
console.log(“fizz”);
}
else if (row % 5==0){
console.log(“Buzz”)
}
else if (row % 3,5==0){
console.log(“FizzBuzz”);
}
console.log(row)
}

1 Like

did fizzbuzz, proud smile here

2 Likes

These exercises were a little tricky, but a good practice. I would get stuck on coding in let output = "". Please let me know if I am incorrect, but the point of let output = “” is to assign some value into the loop whether it be an integer, text value, or string.

so for the fizz buzz assignment I got this far before looking at the answer for help:
for (let x=1; x<100; x++){
if (x%3 ==0) {console.log(“Fizz”);}
if (x%5 ==0) {console.log(“Buzz”);}
console.log(x);
}

This code does not include numbers divisible by 3 and 5 as FizzBuzz, nor does it replace a number divisible by 3 with Fizz… or % 5 with Buzz.

1 Like