Chapter 2 Exercises

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

ChessBoard
for (var i = 0; i<8; i++) {
 var line2show = “”;
 for (var j = 0; j<8; j++) {
  if ((i+j)%2 == 0) { line2show+=" “;}
   else { line2show+=”#";}
 }
 console.log(line2show);
}

1 Like

Hi. You have following errors in your code:
line 2: you have a “)” after var row=0; . Probably a spelling error.

My solutions

Fizzbuzz

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

if (i % 3 == 0 && i % 5 == 0){
document.write(“fizzbuzz”);
document.write ("
");
}

else if(i % 3 == 0) {
  document.write("fizz");
  document.write ("<br>");
}

else if(i % 5 == 0) {
  document.write("buzz");
  document.write ("<br>");
}

else {
  document.write(i);
  document.write ("<br>");
}

}

Chess board

var size = 8;

if (size % 2 !== 0) {
alert(“please give an even boardsize”);
}

for (var i1 = 0; i1 < size/2; i1 ++) {

for (var i2 = 0; i2 < size/2; i2 ++){
  document.write("#" + "&nbsp;");
}

    document.write ("<br>");

for (var i3 = 0; i3 < size/2; i3 ++){
  document.write("&nbsp;" + "#");

}

    document.write ("<br>");
    i2 = 0;
    i3 = 0;
}

1- Looping a triangle

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

2- FIZZBUZZ

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

3-CHESSBOARD

var rowNum=prompt(“Enter the value”);
// var rowNum=8;

for(var row=0;row<rowNum;row++){
var str1="";
if(row%2==0){
for(var col=0;col<rowNum/2;col++){
str1+=" “+”#";
}
}else{

   for(var col=0;col<rowNum/2;col++){
     str1+="#"+" ";
   }
 }
   console.log(str1);

}

function triangleLoop(){
	let numRows = 7;
	for(var row=0; row<numRows; row++){
		let text = "#";
		for(var col=0; col<row; col++){
			text += "#";
		}
		document.write(text + "<br>");
	}
}

function fizzBuzz(){
	for(var num=1; num<=100; num++){
		if(num%15===0) // divisable by 3 & 5 is divisable by 15
			document.write("FizzBuzz <br>");
		else if(num%3 === 0) //divisable by 3
			document.write("Fizz <br>");
		else if(num%5 === 0) //diviable by 5
			document.write("Buzz <br>");
		else
			document.write(num + "<br>");
	}
}

function chessboard(){
	let size = 8;
	let board = ""; 
	for(let row=0; row<size; row++){
		for(let col=0; col<size; col++){`Preformatted text`
			if((row + col)%2 === 0)  // if row is odd start with a space
				board += "&nbsp;";
			else 
				board += "#";
		}
		board += "<br>"
	}
	document.write(board);
}

FizzBuzz
for (var i=1;i<=100;i++){
text1="";
if (i%3==0) {text1=text1+“fiz”;};
if (i%5==0) {text1=text1+“buz”;};
if (i%3!=0&&i%5!=0) {text1=i;};
console.log(text1);
}

Chessboard
size=8;
for (var i=1;i<=size;i++){
text="";
for(var j=1;j<=size;j++)
{
if((i+j)%2==0){text=text+" “} else {text=text+”#"};
};
console.log(text);
}

how can we write Indenting Code in the comments?

Looping a triangle

    let row = "#";
    for (row.length; row.length < 8; row += "#") {
        console.log(row + "\n");
    }

FizzBuzz

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

Chessboard

    let size = prompt("Enter the size of the board: ");
    let board = "";

    for (row = 0; row < size; row++) {
        for (column = 0; column < size; column++) {
            if ((column % 2 == 0 && row % 2 == 0)) {
             board += " ";
            } else if ((column % 2 == 0 && row % 2 == 1)) {
                board += "#";
            } else if ((column % 2 == 1 && row % 2 == 1)) {
                board += " ";
            } else {
                board += "#";
            }
        }
        board += "\n";            
    }

    console.log(board);

After I completed my solutions I took a look at what some other people had done to solve these exercises. There’s some really elegant code here! Well done all! :smiley:

Use the blockquote (" in the above). Just copy and paste your code as is, then highlight it before pressing the blockquote button.
Test it in preview to make sure it renders how you want it.

Really good hustle everyone, many people figured out how to solve the triangle using only one loop, which is of course a way more superiour solution compared to nested loops, well done!

3 Likes

Pyramid

    <script>
        for(let height=1; height <= 7; height++ ){
          let pyramid = '';
          for(let width=1; width <= height; width ++) {
              pyramid += "#";
          }
          console.log(pyramid + "\n");
        }
    </script>

Checkered
>

        board_size = 10;
        for(let row = 1; row <= board_size; row ++){
             let board = "";
             for(let column = 1; column <= board_size; column ++){
                  if ( (row + column) % 2 == 0){
                    board += "#";
                  }else{
                    board += " ";
                  }
             }
             console.log(board);
        }
    </script>
1 Like

Here are my solutions. These were not so easy for me! I hope this blockquote feature of this editor works right and doesn’t mangle this…

Looping A Triangle

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

FizzBuzz

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

Chessboard

  var bsize = 8;
  var board = "";
  for (var col = 0;  col < bsize; col++) {
    for (var row = 0; row < bsize; row++) {
      if ((row+col) % 2 == 0) {board += " ";
      }
      else {board += "#";
      }
    }
    board += "\n";
  }
  console.log(board);

Finally! Done! I had to review, review, AND review…then, google :crazy_face:

Looping a Triangle:

Originally, for the var content = ‘#’ I used the # sign followed by:
for (var i = 0; i < 7; i++) {
content = content + ‘#’;
console.log(content);
}

The only problem is the my triangle started with two ##, then I realized, I needed to omit the #, so here’ s my final answer:
var content = ‘’;

for (var i = 0; i < 7; i++) {
content = content + ‘#’;
console.log(content);
}

FIZZ BUZZ - I think I have some of my hair left on this one. It took me forever to figure, read and re-read, and Yes, Ivan, google search other examples:

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

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

Hey. Here’s my first stab at these.

FizzBuzz:

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

Chessboard:

var size = 8;

for(var row = 0; row < size; row++) {
var toPrint = “”;
if(row % 2 == 0) {
for(var char = 0; char < size; char++) {
if(char % 2 == 0) {
toPrint += " ";
}
else {
toPrint += “#”;
}
}
}

else {
for(var char = 0; char < size; char++) {
  if(char % 2 == 0) {
  toPrint += "#";
  }
  else {
    toPrint += " ";
  }
}

}
console.log(toPrint);
}

Looping a triangle

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

FizzBuzz

for (let number=1; 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 && number%3!=0){
    console.log("Buzz");
  }
  else{
    console.log(number);
  }
}

Chessboard

let string="";
let row="odd";
let count_odd=0;
let count_even=0;
for (count=0; count<8; count+=2){
  if(row=="odd"){
    while(count_odd<8){
      string+=" #";
      count_odd+=2;
    }
    if(count_odd==8){
      string+="\n";
      count_odd=0;
      row="even";
    }
  }
  if(row=="even"){
    while(count_even<8){
      string+="# ";
      count_even+=2;
    }
    if(count_even==8){
      string+="\n";
      count_even=0;
      row="odd";
    }
  }
}
console.log(string);

Triangle

var t="x";
for (let i = 1; i < 8; i++) {
	var sor="";
	for (let j = 0; j < i; j++) {
		sor+=t   
	}
	console.log(sor);
}

FizzBuzz

for (let i = 1; i < 101; i++) {
	textToPrint="";
	if (i%3===0) {
	   textToPrint+="fizz";
	}
	if (i%5===0) {
		textToPrint+="buzz";
	}
	if (textToPrint=="") {
		console.log(i);
	} else {
		console.log(textToPrint);
	}
}

Chessboard

let size=8;
textToPrint=""
for (let i = 0; i < size; i++) {
	for (let j = 0; j < size; j++) {
		if (i%2===0) {
			if (j%2===0) {
				textToPrint+=" ";
			} else {
				textToPrint+="#";
			}
		} else {
			if (j%2===0) {
				textToPrint+="#";
			} else {
				textToPrint+=" ";
			}
		}   
	}
	textToPrint+="\n"
}
console.log(textToPrint);

FizzBuzz

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

}

Chessboard

var size = 8;

var board = “”;

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

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

if((i + x) % 2 == 0)

board += " ";
else
board += “#”;
}
board += “\n”;
}
console.log(board);

FIZZBUZZ

var divThree = “fizz”
var divfive = “Buzz”
i=1;
while(i<=100){
if(i%5==0 && i%3==0) console.log(divThree+divfive);
else if(i%3==0)console.log(divThree);
else if(i%5==0)console.log(divfive);
else console.log(i);
i++;
}

My triangle loop :

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

FizzBuzz v1.0

var num = 1;

do{
if(num%3 !==0 && num%5 !== 0)
{
console.log(num)
}

num++;

if (num%3==0)
{
console.log(“Fizz”);
continue;
}
if (num%5==0)
{
console.log(“Buzz”);
}

}while(num < 100)