Chapter 2 Exercises

Here you go, i tried to describe the process step by step on what it does:

// board size
var size = 8;
// board variable, string type
var board = "";
// 1st loop, until reach size
for (var y = 0; y < size; y++) {
  // 2nd loop, add a value on 1st board line
  for (var x = 0; x < size; x++) {
    // if module operation result is zero, add a space
    if ((x + y) % 2 == 0)
      board += " ";
    // else, add a character
    else
      board += "#";
  }
  // create a new board line
  board += "\n";
}

console.log(board)

Carlos Z

Thank you so much, @thecil :smile:!!

2 Likes

Triangle Loop:
let number = 1;
let result = “#”
while (number <=7) {
console.log(result);
number = number + 1;
result = result + “#”;
}

FizzBuzz loop:
1st Part:

let result = 1;
let counter = 0;
while (counter <100) {
if (result % 3 == 0 && !result % 5 == 0) {
console.log(“Fizz”);
} else if (result % 5 == 0 && !result % 3 == 0) { //Programa imprimiu Fizz para o numero 15 (15 eh divisivel por 5 e por 3, portanto deveria ser Buzz)
console.log(“Buzz”);
} else {
console.log(result);
} result = result + 1;
counter = counter + 1;
}

2nd Part:

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

ChessBoard Loop:
for (let i = 1; i <= 8; i++) {
let output = “”;
if (i % 2 == 0) output += “# # # #”;
if (i % 2 !=0) output += " # # # #";
console.log(output || i);
}

1 Like

wow, well. truth be told I needed a bit of help with these-

Looping Triangle

for (let 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

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

ChessBoard

var board = “”;

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

1 Like

Complete failure… Super frustrating start since I needed to look at all three answers. Will try again later.

Triangle

for (let 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

Looping Triangle

for (var result = “#”; result.length <=7; result = result + “#”)

console.log(result);

1 Like

FIZZ BUZZ

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

1 Like

Chess Board

var board = “”;
for(var i = 0; i < 8; i++){
for(var a = 0; a < 8; a++){
board += (a % 2) == (i % 2) ? " " : “#”;
}
board += “\n”;
}

1 Like

LOOPING A TRIANGLE

let a = "";
for (let count = 0; count < 7; count = count + 1) {
           console.log(a += "#");          
}

FIZZBUZZ

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

CHESSBOARD

let board = "";
let squares = Number(prompt("Enter desired squares per side of board upto a max of 64"));
for (count = 0; count < squares && squares < 65; count++) {
  if (count % 2 == 0)
    board += " #".repeat(squares/2) + "\n";
  else
    board += "# ".repeat(squares/2) + "\n";
}
    console.log(board);

/*A quicker solution though is:
for (let i = 1; i < 9; i++) {
let n = “”;
if (i % 2 == 0) n+= "# # # # “;
else n+= " # # # #”;
console.log(n);}
*/

1 Like

Unfortunately when pasting to the forum the quotation marks are getting slightly screwed up, so when copying the text from forum to console the quotation marks are not recognised and code is seen as invalid when trying to run it. May need to re-enter the quotation marks on the console.

Triangle Loop

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

FizzBuzz

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

   }
   else{
      console.log(i);
   }

ChessBoard

var size = 8
var board = "";
for(let i = 0; i < size; i++){
 for(let a = 0; a < size; a++){
  board += (a % 2) == (i % 2) ? " " : "#";
 }board += "\n";}
 console.log (board);
1 Like

Not sure if I did things the right way, but I came up with some correct (looking) answers. TBH I probably worked on these for 10 - 12 hrs trying to figure it out myself. I should have watched the “How to do exercises” video first, but I wanted to see if I could come up with something that looked correct. I think all of the trial an error taught me a lot!

Brad

Pyramid

var b = “”;
for (let a = 0; a <7; a = a + 1) {
console.log (b += “#”);
}

Fizz/Buzz – Part 1

for (var nu = 1; nu <=100; nu = nu + 1)
if (nu % 5 == 0) {
console.log (“buzz”);
} else if (nu % 3 == 0) {
console.log (“fizz”);
} else if (nu = nu) {
console.log (nu);
}

Fizz/Buzz part 2
for (var nu = 1; nu <=100; nu = nu + 1)
if (nu % 3 == 0 && nu % 5 == 0) {
console.log (“fizzbuzz”);
} else if (nu % 5 == 0) {
console.log (“buzz”);
} else if (nu % 3 == 0) {
console.log (“fizz”);
} else if (nu = nu) {
console.log (nu);
}
Chess Board
var blk = “# # # #”
var wht = " # # # #"
for (var chb = 0; chb <=7; chb = chb + 1)
if (chb % 2) {
console.log(blk);
} else if (chb % 1 ==0) {
console.log(wht);
}

1 Like

First of all, I did,not manage to solve the exercises on my own. For the FizzBuzz exercise I at least managed to go half way. But in the end I consulted the solutions provided by the book. After looking them up they make sense to me.
But how did you go about finding the solutions for these exercises? Thanks in advance for the help.

var size = 7;
for (row = 0; row < size; row++) {
var result = “#”;

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

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

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

1 Like

1. Triangle:

let hash = “#”;
for (let num = 1; num<=7; num++)
{
console.log(hash);
hash = hash + “#”;
}

2. FizzBuzz:

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

    }

3. ChessBoard:

  let size = 8;
  let chess = "";
  let square = "";
  let column = 0;
  for (let row = 0; row<size; row++)
  {
    for( ; column<size; column++)
    {


        if(row%2!==0)
        {
            if(column%2==0)
                {
                    square = "#";

                }
            else
                square = " ";


        }
        else
        {
            if(column%2==0)
                {
                    square = " ";
                }
            else
                square = "#";
        }
        chess = chess + square;
    }
    chess = chess + "\n";
    column = 0;
    square = "";
  }
  console.log(chess);
1 Like
  // Chapter 2 Exercise FizzBUzz
  // First, you create a loop to count 1 to 100, in each iteration you check if the number is divisible by 3 and 5 (print "FizzBuzz"), otherwise
  // you then check if the number is divisible by 3 (print "Fizz"), if not, you then check if that number is divisible by 5 (print "Buzz"), 
  // if neither the previous conditions were fulfilled you then print what number is that in the console.
  for(x = 1; x <= 100; x++){
    if ((x % 3 == 0) && (x % 5 == 0)) {
      console.log("FizzBuzz");
    } else {
      if (x % 3 == 0) {
        console.log("Fizz");
      } else {
        if (x % 5 == 0) {
          console.log("Buzz");
        } else {
          console.log(x);
        }
      }
    }
  }

  // Chapter 2 Exercise Chessboard
  // Creating a loop with an inner loop in constructing the grid (representing the rows and columns) based on the 'size' given (i.e. size = 8).
  // Concatenating a " " or "#" alternately depending if the row number is an even number, the odd number rows gets an opposite alternating pattern of a "#" first then a " " and so on.
  // At the end of each row, a newline character is concatenated at the end.
  let text = "", size = 8;
  for(x = 0; x < size; x++) {
    for(y = 0; y < size; y++) {
      if (x % 2 == 0) {
        if (y % 2 == 0) {
          text += " ";
        } else {
          text += "#";
        }
      } else {
        if (y % 2 == 0) {
          text += "#";
        } else {
          text += " ";
        }
      }
    }
    text += "\n";
  }
  console.log(text);
1 Like

FizzBuzz Loop

var num = 100

for (num=1; num<=100; num++){
  let output = "";

  if(num % 3 ==0 ) output += "fizz";
  if(num % 5 ==0 ) output += "buzz";

  console.log(output || num);

}
type or paste code here

Triangle Loop

var num_row = 7;
for (var row =0; row<=8; row++){
  var toprint ="#";

  for(column =0; column<row; column++){
    toprint +="#";
  }
  console.log("number of"+ row);

}
type or paste code here
1 Like

Code>
Exersie 2.2

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

    var text=i;

    if(i%3==0) {text="Fizz";}

    if(i%5==0) {text="Buzz";}

    if((i%3==0)&&(i%5==0)) {text="FizzBuzz";}

    console.log(text);

}

Exersie 2.3

var width = 8;

var height = 8;

var text = "";

for (var i = 0;i<width;i++){

    for (var j=0; j<height;j++){

        if((i+j)%2==0) {text+=" "} else {text+="#"}

    }

    text+='\n';

}

console.log(text);
1 Like