Chapter 2 Exercises

book excecise 1 - looping a triangle

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

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

Chequer board

I got myself in a right mess with this one, couldnt really figure it out in the end. At first I made a script that made the first line, which was all good. Then trying to implement it onto multiple lines I tied myself in nots going round in circles, I had to look up the answer in the end but this was my incorrect effort by the end -

var boardSize = 8
var showBoard = “”
var hash = “#”
var space = " "

for (var xCounter = 0; xCounter < boardSize; xCounter++){
if (xCounter % 2 == 0){
for (var yCounter = 0; yCounter < boardSize; yCounter++){
if (yCounter % 2 == 0){(showBoard=showBoard+space)
}
else {(showBoard=showBoard+hash)}
}
else {for (var yCounter = 0; yCounter < boardSize; yCounter++){
if (yCounter % 2 == 0){(showBoard=showBoard+hash)
}
else {(showBoard=showBoard+space)}
}
}

}

1 Like

1: for (let m = 1; m <= 100; m++) {
let output = m
if (m % 3 == 0) output = "Fizz ";
if (m % 5 == 0) output = "Buzz ";
if (m % 15 == 0) output = "FizzBuzz ";
document.write(output || m); console.log(output || m);
}
Had such a hard time but after working for awhile i did figure this out! Still now sure how to get the document to look right with the
i can’t seem to place them right to space all the numbers and words right.

2: 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);
not going to lie i could not figure this out. it really helped me though once i peaked i figured it out.

1 Like
  1. Looping a Triangle: This exercise I did it along with Ivan on the video, didn’t put much effort but was nice to recap and get started with.
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);
}
  1. FizzBuzz: I’m proud I could manage to do this one on my own!
for (let current=1; current<=100; current++){

  if (current % 3 == 0 && current % 5 == 0){
      console.log("FizzBuzz");
  } else if (current % 3 == 0){
      console.log("Fizz");
    } else if (current % 5 == 0){
      console.log("Buzz");
      } else{
      console.log(current);
        }
}
  1. ChessBoard: Guys I tried hard thinking how to solve this one but couldn’t :frowning: I had to look at the answer and even once there it took a lil while to get what was done in the code! Any feedback on how to approach these kind of exercises? The fact that says to print either an empty space or a hashtag got me with quite a challenge.
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";
}
1 Like

Triangle

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

FIZZBUZZ

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

let width = parseInt(prompt(Enter the width));
let height = parseInt(prompt(Enter the height));

let text = “”;

for(let i = o; i < width; i++){
if ((i+1)% 2 == 0 ){
for(let i = o; i < height; i++){
text += “#”;
}
console.log(text);
text = “”;
}
else {
for(i = 0; i < width; i++){
text += “#”;
}
console.log(text);
text = “”;
}
}

1 Like

@CharlieMo

Make sure to write your commands in the same line instead of clicking enter to go to the next line. So type all the lines of code at once and then execute them.

Happy Learning!

  1. Triangle loop.

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

  1. FizzBuzz

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

  1. Chessboard:-
    // I used “[ ]” to specifically show the white squares, and kept the size of to board to floating.//

var s=parseInt(prompt(“Enter the board size”));
console.log(“size of the chessboard is “,+s,“X”,+s);
for(var row=1; row<=s;row++)
{var toPrint=””;
for(var column=1;column<=s;column++)
if(row%2==0)
{if(column%2==0)
var toPrint=toPrint + “[]”;
else var toPrint= toPrint+ “#”;}
console.log(toPrint);}

1 Like

FizzBuzz I believe I did pretty well with this on my own… Chessboard was a different story but I think I understand both answers enough to move on:

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

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";
}
2 Likes

Chessboard: not sure this code is 100% correct

let row1=(’ # # # #’);
let row2=(’# # # #’);
let counter = 1;

while (counter <=4) {

console.log(row1);
console.log(row2);

counter = counter+1

}

2 Likes

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

FizzBuzz
while (number<limit){
if (Number.isInteger(number/3)){
console.log(“Fizz”);
}
else if (Number.isInteger(number/5)){
console.log(“Buzz”);
}
else {
console.log(number);
}
number ++;
}

Fizzbuzz part 2
var number = 1;
var limit = 101;
while (number<limit){
if (Number.isInteger(number/3) && Number.isInteger(number/5)){
console.log(“FizzBuzz”);
}
else {
console.log(number);
}
number ++;
}

1 Like
  1. FizzBuzz solution:
    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);
    }
    This code reads setting var as “i = 1” then the condition to be less than 101 which the console will run 100 times and no more then “i ++” will increment var through the loop. The if statement has a condition that if var mod 15 is equal to zero - no remainder - then it is divisible by both 5 and 3 and it prints “FizzBuzz” to the console as the loops continues. The next if statement places a condition that " i mod 3 is equal to 0",then it prints to the console “Fizz” continuing through the loop the next if statement checks that " i mod 5 == 0 " if true it prints on the console “Buzz.” :Lastly the else condition checks that if the first one or the second condition are false it should print(i), var to the console to the end of the loop.

  2. Chessboard:
    var size = 8;
    var block = ‘#’;
    var space = ’ ';

for (var i = 1; i <= size; i++) { /
var line = ‘’;

for (var y = 1; y <= size; y++){
if (i%2) {
if (y%2) {
line = line + space;
} else {
line = line + block;
}
} else {
if (y%2) {
line = line + block;
} else {
line = line + space;
}
}
}

console.log(line);
}

// Outer for loop creates a new line** after each cycle of the inner for loop
   for (var i = 1; i < size; i ++) {

     for (var y = 1; y <= size; y++)
       //The nested for loop checks if the condition for the y variable and the condition for the i variable is true then executes a line + a block  or if is false then prints a line + space. Otherwise it prints a line  at the end of the loop.
1 Like

FizzBuzz

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

  if(i % 15 == 0){
    document.write("<h1>FizzBuzz</h1>");
  } else if (i % 3 == 0) {
      document.write("<h1>Fizz</h1>");
  } else if (i % 5 == 0) {
     document.write("<h1>Buzz</h1>");
  } else {
    document.write("<h1>" + i +"</h1>");
}

}
1 Like

I changed the single quotes in row1 and row2 to double quotes and it worked for me.

Thanks for this easy solution, I was thinking way to complicated on this one. :sweat_smile:

3 Likes

FizzBuzz
for(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”)
}
if(i%3 !==0 && i%5 !==0){
console.log(i)
}
}

ChessBoard Loop

var size = 8

for (i = 0; i < size/2; i++){
  console.log('\n', '# # # # ')
  console.log('\n', ' # # # #')
}
1 Like

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

if (i % 3==0){
  document.write("Fizz");
  continue;
  else if (i % 5==0 && i % 3==0) {
      document.write("FizzBuzz");
    continue;
  };
  else if (i % 5==0) {
      document.write("Buzz");
    continue;
  };
else {
  document.write(i);

};
};

let size = 8;

let board = “”;

for (i = 0; i < size; y++) {
for (j= 0; j < size; x++) {
if ((i + j) % 2 == 0) {
board += " ";
} else {
board += “#”;
}
}
board += “\n”;
}

console.log(board);

1 Like

1 Triangle loop

While Loop

var str = “#”;
while ( str.length < = 8) {
console.log (str)

str += “#”;

}

For Loop

let char = “#” ;
for( let i = 0; i < 8; i++) {

console.log( char );
char += “#”;

}

2 FizzBuzz Loop

for (let a = 1; a <= 100; a++) {
if ((a % 15 === 0) && (a % 5 === 0)) {
console.log(“Fun”);
} else if (a % 3 === 0) {
console.log(“This”);
} else if (a % 5 === 0) {
console.log(“is”);
} else {
console.log(a);
}
}

3 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

For the FizzBuzz exercise I used a “for” statement with a nested if/if else/else statement in order to count 0 to 100, while checking each counter for if it’s divisible by A) 3 && 5 or B) 5 or C) 3 or none.

For the chessboard exercise, I used one while statement to create a string to be used for each line based on the grid size. I then created a second while statement that printed each line in the grid, using an “if” statement to subtract the first space in each line that was divisible by 2 (even).

1 Like

FizzBuzz Loop

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

ChessBoard Loop

for(var i = 1; i <= 4; i++){
console.log(" # # # #"); console.log("# # # #");
}

1 Like

FizzBuzz

    <script>
      var num = 100;
      for(i=1;i<=num;i++){
        if(i%15==0){
          console.log("FizzBuzz");
        }
        else if(i%5==0){
          console.log("Buzz");
        }
        else if(i%3==0){
          console.log("Fizz");
        }
        else{
          console.log(i);
        }
      }
    </script>

Checker pattern

<script>
      var rows = 8;
      var cols = 8;
      iRows=0
      var toPrint = "";
      for(iRows=0;iRows<rows;iRows++){
          for(iCols=0;iCols<cols;iCols++){
            if(iCols%2==0&&iRows%2==0){
              toPrint+=" ";
            }
            else if(iCols%2==1 && iRows%2==0){
              toPrint+="#";
            }
            else if(iCols%2==0 && iRows%2==1){
              toPrint+="#";
            }
            else if(iCols%2==1 && iRows%2==1){
              toPrint+=" ";
            }
          }
          toPrint+="\n"
      }
      console.log(toPrint)

    </script>
1 Like

Solution to Chapter 2 Exercise FizzBuzz
for(var i=1; i<=100; i++) {
if((i % 3 == 0) && (i % 5 == 0)) {
console.log(“FizzBuzz”);
i=i+1;
}
if(i % 3 == 0) {console.log(“Fizz”);}
else {
if(i % 5 == 0) {
console.log(“Buzz”);}
else {console.log(i);}}
}

Solution to Chapter 2 Exercise Chessboard
let rows = 7;
let patternTotal = " ";
let patternX = " ";
let patternY = " ";
let patternZ = " ";

patternX = " ";
patternY = “#”;
patternTotal = patternX;

for (let numberRows = 1; numberRows <= rows; numberRows ++) {
patternTotal = patternX;

for (let number = 1; number < rows; number ++) {
	patternZ = patternX;
	patternX = patternY;
	patternY = patternZ;
	patternTotal = patternTotal + patternX;
}
console.log(patternTotal);
patternTotal = patternX;
if (rows % 2 == 1){
   	patternZ = patternX;
	patternX = patternY;
	patternY = patternZ;}

}

1 Like

Solution to Chapter 2 Exercise Chessboard and FizzBuzz.

let num = 8;

for (let i = 4; i < num; i++){

let stars = "#" + " " + "#" + " " + "#" + " " + "#" + "\n" + " " + "#" +  " " + "#" + " " + "#" + " " + "#";


for (let  p =  0;  p  <  i;  p++){

stars += " ";
}
console.log(stars);

}

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

1 Like