Chapter 2 Exercises

  1. Looping a triangle
    Schermata 2020-04-08 alle 15.40.18

  2. FizzBuzz
    Schermata 2020-04-08 alle 15.58.39

  3. Chessboard
    Schermata 2020-04-08 alle 16.54.50

2 Likes
  1. Looping a Triangle
    image
  2. FizzBuzz
    image
  3. Chessboard
    image
1 Like

Looping a 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

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

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

}

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

ANSWER 1 -
var r = 4 ;
var b = “#” ;

for (x = 0 ; x < r ; x++) {
for (y = 0 ; y < x ; y++){
b += “#” ;
} console.log (b) ;
}

Answer 2 :
var m = 101;
for( var n = 1 ; n < m ; n = n + 1) {
if (n % 15 == 0){ console.log (" fizzBUZZ “) ;}
else if (n % 5 == 0 ){ console.log (“BUZZ”) ;}
else if (n % 3 == 0) { console.log (” FIZZ ") ;}
else { console.log (n);}
}

Answer 3 -
var board = 8 ;
var z = “”;

for ( var x = 0 ; x < board ; x++){

for ( var y = 0 ; y < board ; y++){
if ( (x+y) % 2 == 0 ) { z += " " ; }
else { z += “#” ;}
}

z += “\n” ; }
console.log (z) ;

1 Like

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

#2 FizzBuzz

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

#3 Checkerboard

var size = 8;
var space = “”;

for (var x = 0; x < size; x++){
for (var y = 0; y < size; y++){
if ((x + y) % 2 == 0)
space += " ";
else
space += “#”;
}
space += “\n”;
} console.log(space);

1 Like

Triangle

let myString="#"
for (counter = 1 ; counter <= 7; counter++) {
  console.log(myString);
  myString+='#'
}

One line version of Triangle

for(myString = '#' ; myString.length <= 7 ; myString += '#') {console.log(myString)}

FizzBuzz

for (counter=1;counter<100;counter++){
  switch (true) {
    case (counter % 3 == 0 && counter % 5 == 0) :
      console.log("FizzBuzz");
      break;
    case (counter % 3 == 0) :
      console.log("Fizz");
      break;
    case (counter % 5 == 0) :
      console.log("Buzz");
      break;
    default : 
      console.log(counter)
  }
}

Chessboard

let size = 8;
let myBoard = "";
for (row = 0; row < size; row++) {
  for (column = 0; column < size; column++) {
    if (row % 2 == column % 2) {
      myBoard += " "
    } else {
      myBoard += '#'
    }
  }
  myBoard += '\n'
}
console.log(myBoard)
1 Like

@ivan
Problems with one solution of FizzBuzz

Please note that the break statement after the first case has been omitted on purpose, in order to get “FizzBuzz” for a value that is divideable by 3 and by 5 as well! In this version of my script I tried to get the output at the HTML document and at the console. However the results of the HTML document and the console were different! How could this be? Please refer to the screenshot below!

    for (counter = 1 ; counter < 100 ; counter++){
       switch (true) {
        case (counter % 3 == 0) :
          console.log("Fizz");
          document.write("Fizz");
          // No break statement in this line in order to get
          // "FizzBuzz" if a value is divideable by 3 and 5
        case (counter % 5 == 0) :
          console.log("Buzz");
          document.write("Buzz");
          break;
        default :
          console.log(counter);
          document.write(counter);
      }
      document.write("<br>");
    }

1 Like

Yes man this was a little crazy task for a beginner, kind of want to stop JS before even starting hehe… No wonder if i couldn’t resolve by myself, maybe after a few book on javascript it can be done…

However i still looked after, so i copy here what i found, analyzed for quiet a while, and tried to understand…

FizzBuzz:

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

Chess:

var board = “”;
var rows = 0;
while(rows < 8) {
var cols = 0;
var previousHashed;
if(rows % 2 === 0 ) {
previousHashed = true
} else {
previousHashed = false;
}
while(cols < 8) {
if(previousHashed) {
board += " ";
} else {
board += “#”;
}
previousHashed = !previousHashed;

    cols++;
}

board += “\n”;
rows++;

}

console.log(board);

1 Like

Hi. I corrected the code in your post, could you try it now and see if the output is correct now?

Ivo

1 Like

FizzBuzz:

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

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

Hi.
You have forgotten the brackets { } in your FizzBuzz answer. Do you see it?
besides that, it is very good!

Ivo

QUESTION 1 :
var t = 5 ;
var p = “*”;

for ( var a = 0 ; a < t ; a++ ){
var p = “*”;
for ( var b = 0 ; b < a ; b++ ){
p += “*” }
console.log §;
}

vs

var t = 5 ;
var p = “*”;

for ( var a = 0 ; a < t ; a++ ){
for ( var b = 0 ; b < a ; b++ ){
p += “*” }
console.log §;
}

@ivga80 can you please explain why there is a diffrence in between these 2 codes.
I am not to able understand this

1 Like

Exercise chapter 2

Hi guys,

Wow, you all seem like you great programmers already. I have tried all exercises and the only one which I was really confident was the first one. Other two were probably more done by correcting my very unsuccessful attempts. What am I missing or doing wrong? Please share your secret, lol.

Here is my fixed up codes and attempts.

FuzzBuzz

According to the book, I would be out of the interview straight away.

Chessboard

I had fun with this one but still needed a bit of fixing. Is bloody hard.

I’m still in the early stages of my learning. Not giving up quite yet.
Cheers

1 Like

Thanks, I wrote it again on atom from the begin.

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

In the looping a triangle exercise you’ve named the variable “number” and then in the function you call it “rows” resulting in an error

Nice, this means that all the people who copied his answer also is have the same error… :see_no_evil:

Ivo

As a n00b I’m going through different answers, typing them in by hand and playing around with them to see if they execute correctly or not.
On that note I just noted that your FIZZBUZZ program would execute correctly even if you skipped the last bit :slight_smile:

else console.log(count);
count++

Interesting solutions especially the chessboard one
I got a n00b question: What’s up with the console.log(output);
at the end of each assignment?
It doesn’t seem to fill any other function than to leave the statement “undefined” at the bottom of the program

As a freshman going through different people’s answers I appreciate that you took the time to explain your line of thinking.

It’s been 3 days, I do have a headache now :face_with_head_bandage:, I understood the Triangle exercise, then the fizz buzz I got it, but when I compare with the actual answer of the book I was lost again.
The third exercise I can’t understand, maybe i’m not logical enough in my mind … :frowning:

- Triangle
var num_rows = 8;
for (var row = 0; row < num_rows; row++) {

  var toPrint = "#";

  for (var column = 0; column < row; column++) {
    toPrint += "#";
  }

console.log(toPrint);
}

  • FizzBuzz
    My answer:

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

var divisibleByThree = i % 3 == 0;
var divisibleByFive = i % 5 == 0;

if (divisibleByThree && divisibleByFive) {
console.log(“FizzBuzz”);
}
else if (divisibleByThree) {
console.log(“Fizz”);
}
else if (divisibleByFive) {
console.log(“Buzz”);
}

else { console.log(i);
}

}

The book’s answer
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);
}

This I don’t get… where is the condition for “FizzBuzz” … Am I blind :confused:

I could’t solve the third and this is the book answer that I can’t get into my brain

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