Chapter 2 Exercises

I tried to make this exercise in different appoaches.
here I used a function. I know It’s not optimal in this exercise, but great for learning.

//fizzbuzz


function check(value) {

if (value % 3 === 0 && value % 5 === 0){
      return('Fizzbuzz');

    } else if (value % 3 === 0){
      return('fizz');

    } else if (value % 5 === 0){
      return('buzz');

    } else return(value);

}

// Loop

for (var count =0;count<100;count++){

var x = check(count);
console.log(x);
}


greetz ManzO

Here are my solutions - imagine these can be optimised further though…

Triangle

var result = "#";

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

FizzBuzz

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

	if (i % 3 == 0 && i % 5 == 0) {
		console.log("FizzBuzz");
		continue;
	}

	if(i % 3 == 0) {
		console.log("Fizz");
		continue;
	}

	if(i % 5 == 0) {
		 console.log("Buzz");
		 continue;
	}

	console.log(i);
}

Chessboard

var currentSquare = "#";
var size = 20;

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

	var row = "";

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

		currentSquare == " " ? currentSquare = "#" : currentSquare = " ";		
		row += currentSquare;

	}

	console.log(row);
	currentSquare == " " ? currentSquare = "#" : currentSquare = " ";

}

Hey satoshispockamoto, @mmihai80 is right you have an extra parenthesis in your “for” statement. Also so some strange reason your double quotes when declaring the strings are giving me an error:

Maybe try changing them for other double or single quotes. You can check this in the console next time, let me know if it works.

triangle:

let text = “#”
for (var i = 0; i < 7; i++) {
console.log(text);
text += “#”
}

My Solution

Looping a triangle
var nRows = 7;
for(var row = 0; row < nRows; row++){
var toPrint = “#”;

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

**_FizzBuzz_**

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

**_Chessboard_**
var size = 8;
var 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

what is .lenght? not working here …

triangle:

      let rows = 7;
      let print = "#";
      while (print.length <= rows) {
        console.log(print);
        print += "#";
      }

fizzbuzz:

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

FizzBuzz

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

Chessboard

const size = 8;

const width = size, height = size;

let string = '';

for (let row = 0; row < height; row++) {
  for (let column = 0; column < width; column++) {
    if (row % 2 === 0) string += column % 2 === 0 ? ' ' : '#';
    if (row % 2 !== 0) string += column % 2 === 0 ? '#' : ' ';
  }
  string += '\n';
}

console.log(string); 
1 Like

It is good, but you forgot a curly bracket my friend in the end of the for.

Looking up, I see that I may have overcomplicated the problems, guess it’s just a matter of practice after all.

Here are my solutions, I’ve toyed a bit on HTML on them to make them a bit more interactive:

  • Looping a Triangle
  • 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”;
}
console.log(board);

I have found bolded line text difficult to understand. Why we wrote (x+y)? Is there any other way in a similar short code?

chessboard:

    let rows = 8;
    let columns = 8;
    let whiteChar = " ";
    let blackChar = "#";
    let textPrint = "";
    let textTmp = whiteChar;

    for (var j = 0; j < rows; j++) {
      for (var i = 0; i < columns; i++) {
        textPrint += textTmp;
        if (textTmp == whiteChar) textTmp = blackChar;
        else textTmp = whiteChar;
      }
      if (textTmp == whiteChar) textTmp = blackChar;
      else textTmp = whiteChar;
    console.log(textPrint);
    textPrint = "";
    }
1 Like

Very long code by Kornilov. :hatching_chick:

1 Like

Looping Triangle

var hash = “#”
for( var row = 0; row < 7; row ++){
console.log(hash)
hash = hash + “#”
}

Fizz Buzz

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

Chess Board

height = prompt("Hight"); width = prompt("width"); var square = "#"; var current = 1; size = 1 row = 1 do{
do { 
if(current == 1){ 
	square += " "; 
	current = 0;
}else {
	square += "#";
	current = 1;
	}
row++;
}while(row < width);	

console.log (square);

 if(current == 1){
	square = "#";
	current = 1;
	}else {
	square = " ";
	current = 0;
	}

row = 1;
size++
}while (size < height);

The first bit, where you create a chessboard was easy however creating a chess board any size was a lot more tricky. Also found the above works in crome but not ie and only worked with a page refresh.
Also didn’t know about the new line tag "\n"
.

Triangle:

        for(var counter = 0; counter < 7; counter++){
        var hash = "#"

        for(var checker = 0; checker < counter; checker++){
          hash += "#";
        }

          console.log(hash);

        }

FizzBuzz:

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

Checkerboard:

     var size = 8;
     var 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);

I was thinking to do it exactly like this but I could’n find this condition “if ((row + col) % 2 == 0)” in my brain! this is so simple and brilliant at the same time. thank man

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

ChessBoard

let odd = " # # # # \n"
let even = “# # # # \n”

for (i = 0; i <= 8; i++){
if (i%2 == 0){
console.log(even);
}else{
console.log(odd);
}
}

Triangle

var space = " “;
var row = 0;
while (row < 7){
console.log(space+=”#");
row++;
}

FIZZBUZZ

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

###ChessBoard###

var size = 20;
for (row = 0; row < size; row ++){
if (row%2==0){
console.log("#".repeat(size/2));}
else {
console.log("#
".repeat(size/2));
}
}

Hey gang, here are my solutions to fizzbuzz and the chessboard game. If anyone needs help or would like me to provide an explanation to my solutions, please feel free to DM me!

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

// Chessboard
  var gridSize = 8; // change this to any number
  var hash = "#";
  var whitespace = " ";
  var temp = "";

  for (var i = 0; i < gridSize; i++) {
    for (var j = 0; j < (gridSize/2); j++) {
      if (i % 2 == 0) {
        temp = temp + whitespace;
        temp = temp + hash;
      }
      else {
        temp = temp + hash;
        temp = temp + whitespace;
      }
    }
    console.log(temp)
    temp = "";
  }