Chapter 2 Exercises

Looping a triangle
Looping A Triangle

FizzBuzz
FizzBuzz

Chessboard
Chessboard

The FizzBuzz was the easiest to understand in the mind or to know what to do but the hardest to get translated to code for me. It still has not left my mind yet, lol. Studying different sources and mediums plus the forum helped tremendously. I’m not so sure about the indentation “flaws” and “inefficiency” patterns an adept programmer might locate.

2 Likes

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

var chessBoard= '',
    size= 8,
    c;

for(var i = 0 ; i < size ; i++) {
  c= i%2 ? '# ' : ' #';
  for(var j = 0 ; j < size/2 ; j++) {
    chessBoard+= c;
  }
  chessBoard+= '\n';
}

console.log(chessBoard);
1 Like

Love how you wanted to dig deeper and find out the peculiarities of this language. Kudos to you ! :grin:

Yes, the keyword let allows you to make variables that are scoped in the enclosing brackets only. Whereas, var allows you to scope it in the entire function regardless of brackets. That’s why you can access var variables inside the for loops or if/else blocks.

Happy Learning!

2 Likes

Hi @welowy, Appreciate your effort in writing the code, however, the code provided does not yield to the correct answer. Can you try again or take inspiration from one of the answers in the above thread ?

Also remember to paste the code in code format so that we can easily read your answer.

Thank you.

Happy Learning :slight_smile:

1 Like
var num_rows = 7;
        for(var row = 0; row < num_rows; row++){
          var toPrint = "#";
          for(var column =0; column<row; column++){
            toPrint += "#";
          }
          document.write(toPrint + "<br>");
        }
for (var i=1; i < 10; i++){
          if (i % 15 == 0) document.write("FizzBuzz <br>");
          else if (i % 3 == 0) document.write("Fizz <br>");
          else if (i % 5 == 0) document.write("Buzz <br>");
          else document.write(i + "<br>");
        }
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 += "&nbsp;";
    else
      board += "#";
  }
  board += "<br>";
}
document.write(board);
1 Like

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

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

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

HI @mwollike could you post your answer as code format? please take a look at the picture below

2 Likes

Hi Abuga

Thank you, I didn’t knew that function. I will send them again.

Best regards
Michael

ons. 8. jul. 2020 12.16 skrev Abel Sebhatu via Ivan on Tech Blockchain Academy Forum <[email protected]>:

1 Like
    // Triangle:
    var rows = 7;
  	for (var row = 0; row < rows; row++){
    var hash = "#";
    for(var column = 0; column<row; column++){
  	  hash += "#"
      }
    console.log(hash);
  	}


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


    // Chess
    for (x = 0; x < 8; x++) {
       var hash = " ";
       for (y = 0; y < 8; y++) {
          if ((x + y) % 2 == 0){
             hash += " ";
          }
          else {
            hash+= "#";
          }
       }
       console.log(hash);
    }


2 Likes

Triangle

rows = 7;
symbol = "#";
line = "";

for (step=0; step<rows; step++ ) {
	line += symbol;
	console.log(line);				
}

FizzBuzz

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

CheckerBoard

cols = 4;
rows = 8;
oddLine = "# ";
evnLine = " #";
lineType = oddLine;
line = "";

for (colStep = 0; colStep < rows; colStep++) {
	if (lineType == oddLine) {
		lineType = evnLine;
	} else {
		lineType = oddLine;
	}
	for (rowStep = 0; rowStep < cols; rowStep++) {
		line += lineType;
	}
	console.log(line);
	line = "";
}
1 Like

Great! Thank you for helping!

//FizzBuzz
//-------------

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

//Chessboard  (needed lots of help on this)
//----------------
var size = 8;
var square = "";
for(var row=0; row<size; row++){
  for(var col=0; col<size; col++){
    if((col + row) % 2 == 0){
      square += " ";
    }
     else{
       square += "#";
         }
    }
         square += "\n";
       }
         console.log(square);
2 Likes

FizzBuzz

for (let num = 0; 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 a = 8
let b = " "

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

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

    if((row + col) % 2 == 0){
      b += " ";
    }

else {
 
 b += "#";
  }
 }
  
b += "\n";
}
console.log(b);
2 Likes

Spent quite some time [seriously long] on studying this question.
Absolutely no way I could create it on my own, but this is a simple answer to it that I can read and understand now.
However, I think I’ll be studying FizzBuzz for quite some time, as it’s quite the rabbit hole.

FizzBuzz

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

1 Like

Don’t worry @Kieran , You will surely get it after some practise. Hope to see your answers for the other two questions. If you need help, we are always here. :slight_smile:

Happy Learning!

2 Likes

Thanks Malik - much appreciated. . . I did actually do the first one a few weeks back, and for some reason I found the triangle much easier. . I think I just got a bit overwhelmed with info [and the moving parts and maths of fizzbuzz!] and needed to take a step back and let things settle and take it from the beginning and try to consolidate what I learned up till then. I’m going to start looking at the chessboard next week :+1:

1 Like

//PIRAMID
for (var piramid = “#”; piramid.length<7;piramid+="#") {
console.log(piramid);
}
//FIZZBUZZ
for (var i = 0; i <100; i++) {
if (i%5==0 && i%3==0) {
console.log(“FizzBuzz”);
}
else if (i%5==0 && i%3!=0) {
console.log(“Buzz”);
}
else if (i%3==0 && i%5!=0) {
console.log(“Fizz”);
}
else {
console.log(i);
}
}
//CHESSBOARD
var row = 8
var col = 8

for (var rownr = 1; rownr < row+1; rownr++) {
var chessboard = “”;
for (var colnr = 1; colnr < col+1; colnr++) {
if ((rownr+colnr)%2==0) {
chessboard += “#”
}
else if ((rownr+colnr)%2!=0) {
chessboard += " "
}
}
console.log(chessboard);
}

fizzbuzz:

  var maxzahl = 100
  for(var zahl = 1; zahl <maxzahl; zahl++ ){
    if (zahl%3==0){console.log("fizz");}


  else if( zahl%5==0){console.log("buzz");

                     }

    if(zahl%3==0 && zahl%5==0){console.log("fizzbuzz");}
    else {console.log(zahl);
    } }

chessboard:

var zeilen = 20;
var anzahlZeichen = 20;

var anzahl = anzahlZeichen/2;
 var text = "# ";
  var textAlt =" #";
for(var zeichen = 0; zeichen <zeilen; zeichen++ ){
 
  if (zeichen %2 == 0){console.log(text.repeat(anzahl));}
  
 
  else {console.log(textAlt.repeat(anzahl));
  } }

FizzBuzz

for(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 size = 8;
let result = ‘’;

let row = 1;
while (row <= size) {
let column = 1;
while (column <= size) {
if ((column + row) % 2 === 0) {
result += " ";
} else {
result += “#”;
}
column += 1;
}
result += ‘\n’;
row += 1;
}

HI @Dylan_Lucas could you post your code as code format? please take a look at the picture below.

1 Like