Chapter 2 Exercises

FIZZBUZ

function fizzbuz (target) {
for(var i=1; i <=target; i++) {
linestr =i;
if (i%3 == 0) {
linestr = ‘fizz’;
if (i%5 == 0) {
linestr = ‘fizzbuz’;
}
} else if (i%5 == 0) {
linestr = ‘buzz’;
}
console.log(linestr)
}
return ‘done’.
}
fizzbuz ( )
“done.”
fizzbuz (16)

==================================================

Looping a tringle

function recursetree (pattern,endlevel) {
console.log (pattern);
if (pattern.length < end level) {
recursetree (pattern + ‘#’, endlevel);
}
}
recursetree (’#’,7)

=============================================

Chessboard

function chessboard ( ) {
var rankest = undefined;
for (var row = 1; row <= 8; row++) {
rankest = ’ ';
for (var col = 1; col <= 8; col ++) {
if ((row + col)%2 == 0) {
rankest += ’ . ’ ;
} else {
rankest += ‘#’ ;
} //switch colors
} // traverse cols
console.log (rankstr)
} // traverse rows
return ‘done.’
} //chessboard ( )
chessboard ( )

1 Like

Off all that I tried the simple solutions worked for me:

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

Chessboard
var chess = 8;
var board1 ="# # # #";
var board2 =" # # # #";
for(i = 1; i <= chess; i += 2) {
console.log(board2 +"\n");
console.log(board1 +"\n");
}

1 Like

Looping a triangle

var numberOfLines = 7;
for(var line = 1; line <= 7; line++) {
     var write = "#";

     for(addToLine = 1; addToLine < line; addToLine++) {
          write += "#";
     }

     console.log(write);
}

FizzBuzz

Looping Triangle

<html>

<head>
  <title>This is a great website</title>


</head>

<body>

<script>

  var rows = 7;
  var toPrint = " ";
  var row = 0;
  for(var row = 0; row < rows; row++){
    toPrint = "#";

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

</script>

</body>

</html>
1 Like

Looping a triangle

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

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

1:
for (var i = 1; i<100; i++) {
var output = “”;

if( i % 3 == 0) { output += “Fizz”; }
if( i % 5 == 0) { output += “Buzz”; }

if(output == “”) { output = i; }

console.log(output);
}

2:
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

Hi everyone, see my attached Code. Could someone explain to me why I am getting syntax Error and points to my 1st and if I delete those then it moves down to for the Error message.

Should I only be entering the code itself without all the other stuff?

<html>

<head>
  <title>This is a great website</title>


</head>

<body>

<script>

function FizzBuzz(value1, value2){
  let returnValue = "";
for(let a = 0;a<=100;a++){
    if (a%value1 == 0 && a%value2 == 0){
    returnValue += 'FizzBuzz ';
  }
  else if (a%value1 == 0){
  returnValue += 'Fizz ';
  }
  else if (a%value2 == 0) {
     returnValue += 'Buzz ';
   }
   else {returnValue += a + " ";
   }
 }
 return returnValue;
}

</script>

</body>
</html>
1 Like

Okay, this one worked. YouTube helped me with this one but I want to still find out what is wrong with the above messages code. See attached.

FizzBuzz

<html>

<head>
  <title>This is a great website</title>


</head>

<body>

<script>


for(var a = 0;a <= 100; a++){
    if (a % 3 == 0 && a % 5 == 0){
  console.log('fizzBuzz')
  continue
}
if (a % 3 == 0){
  console.log ('Fizz')
  continue
}
    if (a % 5 == 0){
      console.log('Buzz')
    continue
  }
  console.log(a)
}

</script>

</body>
</html>
1 Like

I found this code on YouTube as well and it appears to be a lot shorter than most which is usually best cause time is money. I can say this one is a head scratcher but I will sit down and look at some others codes and see if I can get it to make sense. :slight_smile:

Chess Board

<html>

<head>
  <title>This is a great website</title>


</head>

<body>

<script>

let chessBoard = '# # # #'
let chessBoard_2 = ' # # # #'
for (let a = 0; a < 4; a++){
  console.log(chessBoard)
  console.log(chessBoard_2)
}



</script>

</body>
</html>

Ohhhh, I get it now. It counts each chessboard and chessboard_2 as 1 so to get 8 rows you would only need 4 of each line… :wink: lol smh at myself.

2 Likes

These were a little challenging Did about halfway on them before I scrolled through here for help and got them to work in the console .

Fizz Buzz

var number =100
for( let number=1; number<=100; number++){
     let x=“ “;
if (number%3==0) x = “fizz”;
if(number%5==0)  x = “buzz”;
if(number%3==0 && number%5==0) x=“fizzbuzz”;
console.log( x || number);}

//Chessboard 

let size = 8;
let space = “ “;

for( let row = 0; row < size; row++){
     for ( let column =0; column < size; column ++){
     if ( (row + column ) %2 ==0){
           space += “ “;
   } else { 
           space += “#”;  
    }
}
    space += “\n”;
}
console.log(space);
         
2 Likes

FizzBuzz

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

CHESSBOARDLOOP

  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

Looks like I could have simplified my solutions a little bit, but happy with the results of my first code challenge :smiley:

FIZZBUZ

<script>
      for(FB = 1; FB<101; FB++) {

        if(FB%3==0 && FB%5==0) {
          console.log("FIZZBUZZ");
          continue;
        }
        if(FB%3==0) {
          console.log("FIZZ");
          continue;
        }
        if(FB%5==0) {
          console.log("BUZZ");
          continue;
        }

        console.log(FB);

      }

    </script>

CHESSBOARD - had a little bit more trouble with this one

    <script>

      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);
      </script>
1 Like

Looping a triangle:

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

FizzBuzz:

<body>
  <script>
  for(num=1;num<101;num++){
    if((num % 3==0)||(num % 5==0)){
      if(num % 3==0){
        if(num % 5==0){
          console.log("FizzBuzz");}
          else{console.log("Fizz");}
      }
      if(num % 5==0){
            if(num % 3==0){
              console.log("FizzBuzz");}
              else{ console.log("Buzz");}
        }
      }
    else{ console.log(num);}
    }
  </script>

Chessboard:

  <script>

var a=8;
var b=8;
let write;
for (i=a;i>0;i–){
if (i % 2) {
write=" “+”#";
for (j=b;j>2;j-=2) {

          write+=" "+"#";

      }console.log(write);

}
else {
write="#"+" ";
for (j=b;j>2;j-=2) {

          write+="#"+" ";

        }console.log(write);

}
}

Hi Mat,

Good solution for the Chessboard exercise. However, when I tested it, the first line would be one space off. I think it is due to a space character in the statement.

An empty string should fix it.

Cheers!

Eric

FizzBuzz (only JavaScript codes within script tags are shown)

  for(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){
      console.log("Buzz");
    }
    else {
      console.log(number);
    }

  }

Chessboard (JavaScript codes only)

  let size = 8;
  let pattern_1 = " #";
  let pattern_2 = "# ";
  for(row = 1; row <= size; row++){
      // Odd rows
      if(row%2 !== 0){
        if(size%2 === 0){
          console.log(pattern_1.repeat(size/2));
        }
        else{
          console.log(" " + pattern_2.repeat(size/2));
        }
      }
      // Even rows
      else{
        if (size%2 === 0){
          console.log(pattern_2.repeat(size/2));
        }
        else{
          console.log("#" + pattern_1.repeat(size/2));
        }
      }
  }

</script>

I read solutions by other people and found that solutions using an X-Y-axes approach should work better than my solution.

Eric

Good work! Wish I could help you. This is very difficult!

1 Like

let toPrint = " #";
for(numberRow = 0; numberRow < 8; numberRow++) {
if (numberRow % 2 === 0) { toPrint = " #";} else { toPrint ="#";}
for(i=0; i<8; i++) {
toPrint += " #";
}
console.log(toPrint);

Looping a triangle

var numberOfLines = 7;
for (var line = 1; line <= 7; line++) {
     var write = "#";

     for (addToLine = 1; addToLine < line; addToLine++) {
          write += "#";
     }

     console.log(write);
}

FizzBuzz
This was very difficult for me. Needed lots of help. Trial and error really helped!

var numberLine = 100;
for (var line = 1; line <= 100; line++) {

     if (line % 3 == 0 && line % 5 != 0) {
          console.log("Fizz");
        }

     if (line % 5 == 0 && line % 3 == 0) {
          console.log("Buzz");
        }

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

        console.log(line);
}

Chessboard
I couldn’t figure this one out without tons of help.

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

wow this was a struggle took me forever to work out why the fizz’s wern’t replacing the number and instead getting added as well as doh… got there in the end though
Fizbuzz =
for (let 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);

Chessboard =
let boardsize = prompt(“please choose your boardsize” );
let square = “”;
for (let y = 0;y < boardsize; y ++) {
for (let x = 0;x < boardsize; x ++) {
if ((x+y) % 2 == 0) {
square += " ";
}else {
square += “#”;
}
}
square += “\n”;
}

console.log(square);

Hey wondering if someone can provide a little assistance. As of right now i still don’t feel confident/understand the material enough to come up with my own code so i am just trying to understand the solutions being provided for exercises at end of chapter 2. I was able to understand the logic behind the first 2 exercises but when it comes to the code in the chess board exercise
" 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 am having difficulty understanding the logic behind the placement of the { } brackets and why some close before others and why some aren’t closed until the end. If anyone can help it would greatly be appreciated. I have already wasted too much time trying to figure out on my own, thanks!

  1. 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);
	}
  1. FizzBuzz
function fizzBuzz() {
		for(var 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) {
				console.log("Buzz");
			} 
			else {

			console.log(number);

			}

		}
	}

		fizzBuzz();
  1. Chessboard
function cheeseboard(size) {
			var output = "";
			for(var i = 0; i < size; i++) {
				for(var j = 0; j < size; j++) {
					output += i % 2 === j % 2 ? " " : "#";
					}
				output += "\n";
			}
			return output;
		}
		console.log(cheeseboard(8));