Chapter 2 Exercises

Looping Triangle:

for (var line = “#”; line.length < 8; line += “#”) console.log(line);

FizzBuzz

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

1 Like

var numrows = 7;
for(var row = 0; row<numrows; row++){
var toPrint = “#”;
for( var column = 0; column<row; column++){
toPrint+= “#”;
}
document.write( toPrint + “
” );
}

for (let i = 0; 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”);
}
console.log(i);
}

let size = 8
var numrows = 8
for(var row = 0; row<numrows; row++){
if(row%2===1){
var toPrint = “# # # # “;
}
else{
var toPrint =” # # # #”;
}
console.log(toPrint);
}

1 Like

Looping Triangle:

var stars = " ";

var counter = 0;

while (counter < 9) {

stars = stars + "#";

console.log(stars);

counter++;

}

FizzBuzz:

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

let output = "";

if(n % 3 == 0) output += "FIZZ";

if (n % 5 == 0) output += "BOO";

console.log(output || n);

}

Chessboard;

let size = 8;

let board = "";



for (let row = 0; row < size; row++) {

  for (let col = 0; col < size; col++) {

    if ((row + col) % 2 == 0) {

      board += " ";

    } else {

      board += "#";

    }

  }

  board += "\n";

}



console.log(board);
1 Like

Hi @J_Gen_Art, The syntax over here is wrong. The statements between the if brackets should be a statement with a semicolon and not a comma. If we ought to keep the same logic of yours with correct syntax, it would look like this –

while (y>1){    
    if (count%2===0){    
    chessboard+=hash;
    count--;
    }
    else if (count%line===0)
      {
       chessboard+=newrow;
       y--;
      count--;
      }
    else if (count>1)
   {
    chessboard+=space; 
    count--;
    }
};

On top of that, I believe your logic is a little flawed. Instead of spending more time on this solution, I suggest you take inspiration from many of the student’s top voted answers.

Hope this helps.

1 Like

Looping a triangle

var toPrint = "";
for(row=0; row<7; row++) {
  console.log(toPrint+="#");
}

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 height = parseInt(prompt("Insert the height: ")),
    width = parseInt(prompt("Insert the width: ")),
    board = "";
for (var i = 0; i < height; i++) {
  for (var j = 0; j < width; j++) {
    if ((i + j) % 2 == 0)
      board += " ";
    else
      board += "#";
  }
  board += "\n";
}
console.log(board);
1 Like

1.Looping a triangle
Var d = " ";
Var counter = 0;
While(counter < 7) {
d = d + “#”;
Console.log(d);
counter++;
}

2.Fizzbuzz
for (var i=1; i < 101; i++){
if (i % 15 == 0) console.log(“FizzBuzz”);
else if (i % 3 == 0) console.log(“Fizz”);
else if (i % 5 == 0) console.log(“Buzz”);
else console.log(i);
}
3. Chess board
Var chess = 8;
var board1 = “# # # #”;
var board2 = " # # # #";
for( i = 1; i <= chess; i +=2);{
console.log(board2+"\n");
console.log(board1+"\n");
}

1 Like

Exercise 1.

var pattern = “#”;
let counter = 0;
while(counter<7){
console.log(pattern);
counter++;
pattern+= “#”;}

Exercise 2

This works, but not as elegant as solution in book!

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

Exercise 3

This works, but only returns a square array of size = “size”; it does not allow for independent variation of length and breadth:

let size = 8;
for(n=1;n<(size+1)*size;n++){
if((n+1)%(size+1)==0){
line=line+"\n"
}
else if(n%2!==0){
line=line+"#";
}
else {
line = line + " ";
}
}
console.log(line);

1 Like

That’ll do too. For the later exercises, just make sure you do the question as intended or else it will be hard to evaluate quickly.

Happy Learning!

Thanks Malik! Appreciate your review.

1 Like

FizzBuzz:
(Researched the function option, since this is asked in many interviews with options of using other numbers/values)

function FizzBuzz(value1, value2){
	let returnValue = "";
	for(let i = 1; i <= 100; i++){
		if(i%value1==0 && i%value2==0){
				returnValue += "FizzBuzz ";
			}
			else if (i%value1==0){
				returnValue += "Fizz ";
			}
			else if (i%value2==0){
				returnValue += "Buzz ";
			}
			else{
				returnValue += i + " ";
			}
		}
		return returnValue;
}
console.log(FizzBuzz(3, 5));

Chessboard (I hope the explanation on the bottom helps others. I learned it from a video, then typed it down. It helped me understand the approach. I need to understand the reasoning behind the code.



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

/*

// set the variables:
let size = 8;
// sets a limit for the number of repetitions in the loop

let board = "";
// sets a blank placeholder to be filled with the if statements

for (let y = 0; y < size; y++) {
// y represents the vertical axis aka the rows. This creates the loop for the rows.

for (let x = 0; x < size; x++) {

// x represents the horizontal axis aka the columns. This creates the loop for the columns.
// note: this is a for loop inside another for loop.
// the “for loop of x” must complete its entire run from 0 to 8 before the “loop of y” can start its next loop, where y++

	if ((x + y) % 2 == 0){
		board += " ";

// when we set x + y, every second number will be divisible by 2, therefore,
// every second character will execute the " " space character,
// and each new line will alternate between and even/odd number
// board += " " -> (same as) board + " "
// we adjusted our variable to add " " space to it

NOTE: what “i%2==0” does is it determines if a number is even (==1 would determine if it’s odd). So it takes the value of i and divides by 2, then it compares the remainder against the value of 0.

	} else {
		board += "#";
	}

// for every other number not divisible by 2(odd number), “#” will be executed

	}
	board += "\n";
}

// \n creates a new line
// board += “\n” is the same as -> board + “\n”
// this statement is attached to “for (let y = 0; y < size; y++)”, therefore
// it ensures that “the loop of x” runs 8 iterations (repetitions), aka 8 characters across,
before breaking into a new line.

console.log(board);
*/

3 Likes

that’s a great explanation for the chessboard! thank you.

1 Like

FIZZBUZZ SOLUTION

for(var 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 SOLUTION

// Variables initialization

  var size=8;
  var row1="";
  var row2="";

// Creation of the 2 types of rows

  for (var i=1; i<=size; i++) {
    if (i%2==0) {
      row1=row1+"#";
      row2=row2+" ";
    } else {
      row1=row1+" ";
      row2=row2+"#";
    }
  }

// Printing of the chessboard

for (var k=1; k<=size; k++) {
  if (k%2==0) {
    console.log(row2);
  } else {
    console.log(row1);
  }
}
2 Likes

I. # PYRAMID.
var num_rows = 7;
for (var row = 0; row < num_rows; row++){
var print = “#”;
for (var column = 0; column < row; column++){
print += “#”;
}
console.log(print);
}

II. FIZZBUZZ.
for (var i = 1; i < 101; i++){
if (i % 15 == 0)
console.log(“FizzBuzz”);
else if (i % 3 == 0)
console.log(“Fizz”);
else if (i % 5 == 0)
console.log(“Buzz”);
else console.log(i);
}

III. 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);

1 Like

Chapter 2 Exercises

My variant for solutions to presented tasks:

Triangle:

for (let line = “#”; line.length < 26; line += “##”)
console.log(line);

FizzBuzz:

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

I’ve got a looong way to go with JavaSript skills…

G.

1 Like

Anyone struggling with that? Of course I understand how it works when I see it and analyse it, but I didn’t have a clue where to start when I saw the FizzBuzz. Seams that I’m the only one and everyone is just posting answers and no one talk about how much difficult it is for a newbie. I don’t know maybe I’m not good enough for that. Just feel very discouraged cos everyone else seam to have the answers and I don’t really want to copy paste cos that’s not the point.

Hi @Kamil37,

These questions are not easy. They are meant to be hard so that you understand how the programming mindset really is. All the above answers have been posted after hours of practice and debugging. Do not worry, we are all on the same boat. Just keep persisting. We are all here for you. :slight_smile:

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

for (let 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);
}
}

for (var row=1; row<=8; row++){
var toPrint = “”;
for (var col=1; col<=8; col++){

if (row%2==0){
//If the row is even start with a black square
if (col%2==0){
//If the column is even, white square…
toPrint+=" “;
}
//If only the row is even, black square…
else(toPrint+=”#");
}
//If the row is odd start with a white square
else if (col%2==0){
//If the column is even, black square…
toPrint+="#";
}
else (toPrint+=" ");
//If only the row is even, white square…
}

console.log (toPrint);
//Print the line to the console

}

fizz buzz
var f = 100;
for(var s = 1; s <= f; s++) {
var fz = “Fizz”;
var bz = “Bizz”;
if( s % 3 == 0 && s % 5 == 0) {
console.log(fz+bz)
}
else if ( s % 3 == 0 ) {
console.log(fz)
}
else if ( s % 5 == 0 ) {
console.log(bz)
}
else {console.log(s)
}
Chessboard
var sz = 8;
var space = “”;

for ( var r = 0; r < sz; r++) {
for ( var c = 0; c < sz; c++) {

  if ((r+c)%2 == 0){space += " ";}
  else {space += "#";}

}
space += “\n”;
console.log(space)
}
FizzBuzz was manageable to understand and code. Chessboard was rough. I think I got the result. However, I’m not sure how manipulatable the code is to meet the exercise’s requirements.

Stars
for(counter = 0; counter < 11; counter++){
document.write(counter+"
");}

  counter = 0
  while(counter<11){
    star="*"
    for(stars=0;stars<counter;stars++){
      star=star+"*"
    }
    document.write(star+"<br>")
    counter ++;
  }
  document.write("<br><br>")
  number = 0

  while(number != 10){
    counter = 0;
    number = parseInt(prompt("Give me a number"));
      while(counter<(number+1)){
        document.write("The number is : "+ counter + " moving up to " + number +"<br>")
        counter ++
      }
  ;}

FIZZ BUZZ
result = “”
for(count=0;count<101;count++){
if((count%3)==0&&(count%5==0)){
result = “FizzBuzz”;
}
else
if((count%3)==0){
result = “Fizz”;
}
else
if((count%5)==0){
result = “Buzz”;
}
else {
result = count;
}
document.write(result + “
”)
}
Chess Board
sDisplay = “#”; // this sets it out to start with a space (–)
bChangeDisplay = true
iRow = parseInt(prompt(“Number of Rows”));
iColumn = parseInt(prompt(“Number of Columns”));

for(row=0; row < iRow ; row ++ ){ // this starts the rows
if((row%2) == 1){sDisplay = “–”;}
else {sDisplay = “#”;} // this should switch the first slot based on even or odd
for(column=0; column < iColumn; column ++){ // this starts the columns

  if(sDisplay == "--")   {sDisplay = "#";}    // this should toggle back and forth
  else                   {sDisplay = "--";}

  document.write(sDisplay);
}                                         // this ends the columns
  document.write("<br>");    // add a line break after the row is filled
}                                          // this ends the rows
2 Likes

Pyramid
var num_rows=7;
for(var row=0; row<num_rows; row++){
var toPrint = “#”;

  for (let column = 0; column < row; column++) {
    toPrint += "#";
    
  }
  console.log(toPrint);
}

FIZZBUZZ
let counter=100;
for (let num = 0; num < counter; num++) {
if ((num%5==0) && (num%3==0)) {
console.log(“FIZZBUZZ”);
continue;
}else if ((num%5==0) && (!num%3==0)){
console.log(“BUZZ”);
continue;
}else if(num%3==0){
console.log(“FIZZ”);
continue;
}
console.log(num);

  }

Chessboard
let size = 8;
let toPrint = “”;

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