# Chapter 2 Exercises

#145

FizzBuzz

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

}

#146

Exercise 1
Found a way to use only one while loop.

``````<html>
<script>
let counter = 0; // starts counter at  0
let garabato=" "; // creates an  empty string
while (counter < 7) {
counter = counter + 1;
garabato=garabato+"#";
console.log(garabato);
}
</script>
</html>
``````

Exercise 2, second part

``````<html>
<script>
let n= 0;
while(n<=100){
n=n+1;
if((0==n%3)&&(0==n%5)){
console.log("FizzBuzz");
}
else if ( (0==n%5)&&(!(0==n%3)) ) {
console.log("Buzz")
}
else if (!(0==n%5)&&((0==n%3)) ) {
console.log("Fizz")
}
else {
console.log(n)
}
}
</script>
</html>
``````

Exercise 3, the second part, where you specified size

``````<html>
<script>

// first parts creates second the line of chessboard, according to given size
let mySize=10; // set the size of chess board
let chunk= "#"+" ";
let n=1; // counter to create  line at even position
let myEvenLine=" ";
while(n <= mySize){
myEvenLine=myEvenLine+chunk; // final state of myLine gives lines at even position
n=n+1;
}

// second part creates chessboard
let counter = 0; // start a counter of even or odd lines
while (counter <= mySize) {
counter = counter + 1;
if(1==counter%2){
myOddLine=" "+myEvenLine;
console.log(myOddLine);
}
else {
console.log(myEvenLine);
}
}
</script>
</html>``````

#147

Triangle
for (row = ‘#’; row.length <= 7; row += ‘#’)
console.log(row);

Fizz Buzz
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
var size = 8;
var board = " ";
for (var col = 0; col < size; col++) {
for (var row = 0; row < size; row++) {
if ((row + col) % 2 == 0) {board += " ";
}
else {board += “#”;
}
}
board += “\n”;
}
console.log(board);

#148

FizzBuzz:

for(let a = 1; a <= 100; a++) { //introducing a variable a
if (a % 5 == 0 && a % 3 ==0) { // logical operator
console.log(“FizzBuzz” + a)
}
else if (a % 3 == 0) { // else if statement
console.log(“Fizz” + a )

} else if (a % 5 == 0)
console.log(“Buzz” + a )

}

ChessBoard:

var size = 8;
var board = “”;

for (var y =0; y < size; y++) { // This basically loops the the x loop below such that it can repeat itself 8 times

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

``````if ((x + y)  % 2 == 0) {
board += " ";
} else {
board += "#"
}
``````

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

#149

Looping a triangle

FizzBuzz

Chessboard

#150

Looping a triangle

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

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

}

Can anyone help me understand the mistake i’m not getting the correct pattern what’s wrong in this code?

#151

Looping a triangle

var num_rows = 7;
for(var row =0; row < num_rows; row++){
var toPrint = “#”;

``````  for(column =0; column < row; column++){
toPrint += "#";

}
console.log(toPrint);
``````

FizzBuzz

``````  for(i = 1; i <= 100; i++){

if ((i % 5 == 0)&&(i % 3 == 0)) {console.log("FissBuzz");}
else if (i % 3 == 0) {console.log("Fiss");}
else if (i % 5 == 0) {console.log("Buzz");}
else {console.log(i);
}
``````

Chessboard

I added more white space to make it look better.

var height = 8;
var width = (height/2);

``````     var col = 0;
var row = 0;

while (row < height) {
col = 0;

while(col < width) {
if ((row % 2) == 0) {document.write('&nbsp;'+'&nbsp;'+'&nbsp;'+'&nbsp;'+"#")}
else {document.write("#"+'&nbsp;'+'&nbsp;'+'&nbsp;'+'&nbsp;')};
col++;
}
document.write("<br>");
row++;
}``````

#152

Question # 1 Eloquent Javascript

I learned:

• .length is handy for using strings in Boolean expressions

• you can increment/decrement string values & not just numbers

`````` for (var line = "#"; line.length <= 7; line += "#")
console.log(line);
``````

Question # 2 Eloquent Javascript

I learned:

• you can use modulus to find the remainder with === 0

• How to wrap my for loop into a function so I could manipulate the argument without having to amend multiple lines of code.

``````  function fizzbuzz(number) {
for (var n = 1; n <= number; n++) {
if (n % div1 === 0 && n % div2 === 0) {
console.log("FizzBuzz");
}
else if (n % div1 === 0) {
console.log("Fizz");
}
else if (n % div2 === 0) {
console.log("Buzz");
}
else {
console.log(n);
}
}
}

var countdown = 73;
var div1 = 2;
var div2 = 7;
fizzbuzz(countdown);
``````

Question # 3 Eloquent Javascript

I learned:

• “\n” adds a new line

• (x + y) % 2 can force odd/even in a loop

• the importance of using variables to make manipulating your code easier

• How to structure your {

``````  var board = "";
var boardSize = 16;

for (var y = 0; y < boardSize; y++) {
for (var x = 0; x < boardSize; x++) {
if ((x + y) % 2 === 0) {
board += " ";
}   else {
board += "#";
}
}
board += "\n";
``````

The following video helped me along a lot:

#153
``````<h2>LOOPING A TRIANGLE</h2>
<script>
for (var i = 0; i < 7; i++) {
document.write("<p>");
for (var j = 0; j <= i; j++) {
document.write("#");
}
}
</script>

<h2>FIZZBUZZ</h2>
<script>
for (var i = 0; i < 10; i++) {
document.write("<p>");
for (var j = 1; j < 11; j++) {
if ((i * 10 + j) % 3 === 0 && (i * 10 + j) % 5 === 0) {
document.write("FizzBuzz");
if ((i * 10 + j) % 10 != 0) {
document.write(", ");
}
} else if ((i * 10 + j) % 3 === 0) {
document.write("Fizz");
if ((i * 10 + j) % 10 != 0) {
document.write(", ");
}
} else if ((i * 10 + j) % 5 === 0) {
document.write("Buzz");
if ((i * 10 + j) % 10 != 0) {
document.write(", ");
}
} else {
document.write(i * 10 + j);
if ((i * 10 + j) % 10 != 0) {
document.write(", ");
}
}
}
}
</script>

<h2>CHESSBOARD</h2>
<script>
var size = 8;
for (var i = 0; i < size; i++) {
document.write("<p>");
for (var j = 0; j < size; j++) {
if (i % 2 != 0) {
if (j % 2 != 0) {
document.write("_");
} else {
document.write("#");
}
} else {
if (j % 2 != 0) {
document.write("#");
} else {
document.write("_");
}
}
}
}
</script>``````

#154
``````1.   // Run code here in the context of Chapter 2
var triangle = "";
for (var i = 1; i < 8; i++){
triangle += "#";
console.log(triangle);

2.   // Run code here in the context of Chapter 2
for (var i = 1; i < 101; i++){
var itemString = "";
if (i % 3 === 0) itemString += "Fizz";
if (i % 5 === 0) itemString += "Buzz";
console.log(itemString || i);

3. // Run code here in the context of Chapter 2

const board_size = 8;

var chessBoard_Print = "";

for (var i = 0; i < board_size; i++){
for (var x = 0; x < 4; x++){
if(i==0){;chessBoard_Print += " #";}
else if(i==2){;chessBoard_Print += " #";}
else if(i==4){;chessBoard_Print += " #";}
else if(i==6){;chessBoard_Print += " #";}
else{chessBoard_Print += "# ";}
}
chessBoard_Print += "\n";
}
console.log(chessBoard_Print);``````

#155

hamza Try this:

1. Put consol.log(hash); after your first closing bracket }.
2. You need to declare the number of row eg. number of row = 7;
3. After the first open bracket { declare var hash+="#"; //Fix your counter issue

#156
1. var triangle = “#”
for(var count=0; count<7; count++){
console.log(triangle);
triangle+="#";
}

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

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

#157

For Fizz Buzz, I used a while loop and I saw most of the people didn’t used a while loop, so here is how you do it if you are curious;

let counter = 0
while (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)

}
``````

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

#158

Thanks for the help
But I still have few doubts

1. Can you explain me the reason for putting console.log(hash) after the closing curly bracket ?
2. Why can’t I just var hash="#" before the loop?

#159

Hi Hamaza,

1. You are query 2 for loops (1 outer loop and 1 inner loop) for the triangle project but in your coding, your output statement wrote to display 1 inner loop. You need to output the result of both rows and columns to get the correct output to the browser.

2. You can declared hash string anywhere but it won’t work as the same as you declared in side the for loop. This is because your method work only within the nested for-loop. If you declared outside the for loop it wont get call and therefore, the counter would not able to add to your hash string variable.

I think it is best if you watch Ivan video for this exercise :). He explained very well for newbies like us. He use very similar method like you.
https://ivanontech.teachable.com/courses/287053/lectures/5234447

#160

FizzBuzz

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

Chess Board

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

#161

@foreveryoungcryptos thanks for the help

#162

Thanks, I was lazy.
Always go for the easiest solution.

#163

FizzBuzz
I’m happy with how this finally turned out, but it can no doubt be condensed.

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

Chessboard
This one has been very difficult and I wasn’t able
to crack it on my own. Reverted to studying others’ work.

``````var size = 8;
var board = " ";

for(a = 0; a < size; a++){
for(x = 0; x < size; x++){
if((a + x) % 2 == 0){
board += " ";
}
else {
board += "#";
}
}
board += "\n";
} console.log(board)``````

#164

Triangle
var x = 0;
for (var loop = 1; loop <= 7 ; loop++)
{
x = 0;
while(x < loop)
{
document.write("#");
x++;
}
document.write("
");
}

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

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

}
``````

Chess Board

var currentChar = " ";
var gridLine = “”;
var gridWidth = 8;
var gridHeight = 8;
for (var height = 1; height <= gridHeight ; height++)
{
for(var width = 1; width <= gridWidth; width++ )
{
gridLine+= currentChar;
if(width < gridWidth)
{
if(currentChar == “#”)
{
currentChar = " ";
}
else {
currentChar = “#”
}
}

``````   }
gridLine+="\n";
console.log(gridLine);
gridLine ="";
}``````