Chapter 2 Exercises

Hi @mickymax777, Hope you’re having a wonderful day. I would request you to please paste your code in the desired format so that we can evaluate your code and give feedback.

Please take a look at the picture below.

Regards

1 Like

I used the console to basically practice simple functions like the ones I described earlier. When it comes time to write longer, more complex functions, I will need to use Atom. The console will not be so helpful for those. I’ll learn more about those soon enough.

Thanks for the comment!

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

Fizzbuzz
for(number=0;++number<101;console.log(number%5?x||number:x+‘Buzz’))x=number%3?’’:‘Fizz’

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

FIZZ BUZZ

var a = “Fizz”;
var b = “Buzz”;

for(var counter = 1; counter < 101; counter++){
    if (counter % 15 == 0){console.log(a + " " + (a+b))}
    else if(counter % 3 == 0){console.log(a)}
    else if(counter % 5 == 0){console.log(b)}
    else {console.log(counter);}

}

1 Like

CHESS BOARD (not quite)

var num_rows = 8;
var num_columns = 8;

for(var row = 0; row < num_rows; row++) {
for(var column = 0; column < num_columns; column++)
if((row + column) % 2 == 0)
console.log(" “);
else{console.log(”#"); }
}

CHESSBOARDS Input Custom Square Grid

var size = prompt(“Grid Size”)
var num_rows = size;
var num_columns = size;

for(var row = 0; row < num_rows; row++){
var chessboard = “” ;
for(var column = 0; column < num_columns; column++){
if((row+column) % 2 !== 0) {
chessboard += “#”
}
else if((row+column) % 2 == 0){
chessboard += " "
}
}
console.log(chessboard);
}

1 Like

Triangle shape code:
for(let i=0;i<7;i++){
document.write("
");
for(let j=0;j<=i;j++){
document.write("*");
}}

Fizz Buzz Code:
for(let i = 1; i <= 100; i ++) {
if((i % 5 === 0) && (i % 3 !== 0)){
document.write(“Buzz”);}
else if(i % 3 === 0) {
document.write(“Fizz”);
}
else {
document.write(i);
}
document.write("
")
}

3. Board Code:
let binding = 8;
let toPrint = “”;
for(let i = 0; i < binding; i ++){
if((i === 0) || (i % 2 === 0)){
toPrint = " ";
for(let j = 1; j < binding; j ++){
if(j % 2 !== 0){
toPrint += “#”;
} else {
toPrint += " ";}}
//console.log(toPrint0);
}

  else if ((i !== 0) || (i % 2 !== 0)) {
    toPrint = "#";
    for(let k = 1; k < binding; k ++){
      if(k % 2 === 0){
        toPrint += "#";
      } else {
        toPrint += " ";
    } //console.log(toPrint1); }
  }} console.log(toPrint);}
1 Like

Having the most trouble wrapping my head around the last Chess Board exercise. I think its the double loop and modulus Im most hung up on. Why the use of modulus?

Hi @chommp,
Modulus is used to return the remainder of the division between those two numbers.

Happy Learning! :slight_smile:

2 Likes

Looping a Triangle

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

  for(var b = 0; b < a; b++) {
    toPrint += “#”;
  }
  console.log(toPrint);
}

FizzBuzz - Version 1 (Both of these were tough!!)

var f = "FIZZ"; var b = "BUZZ"; for(a = 1; a <= 100; a++) {
 if(a % 3 == 0)
   console.log(f); {
if(a % 5 == 0)
  console.log(b); {
continue;
    }
  }
console.log(a); }

FizzBuzz - Version 2

var count = 1
var f = “FIZZ”; var b = “BUZZ”;
for(list = 1; count < 101; list++){
if(list % 5 == 0 && list % 3 == 0) console.log(f+b);
else if(list % 3 == 0) console.log(f);
else if(list % 5 == 0) console.log(b);
else console.log(count);
count++
}

ChessBoard

var row=8;
  var line1="# # # #";
  var line2=" # # # #";
  for(i = 1; i<=row; i+=2){
    console.log(line2+"\n");
    console.log(line1+"\n");
  }
1 Like

I was able to do the triangle and Fizzbuzz exercises on my own, but was never successfully able to figure out a solution for the chessboard problem on my own. In any case, here are the three solutions (only really truly mine for the first two). I intend to continue with the course and exercises until I can come back and be able to solve the chessboard or other such loop exercises easily on my own at a later date.

Triangle:


let line = “#”

while (line.length <= 7){

console.log(line)

line = line + “#”

}

FizzBuzz:


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) && (number % 3 !== 0))
console.log(“buzz”)
else console.log (number)

}

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

Here are my solutions for the exercise.

FizzBuzz

var n = 1
var f = "Fizz"
var b = "Buzz"

for (n = 1; n <= 100 ; n ++) {
	if ( n % 3 == 0 )
      console.log(f)
  	if ( n % 5 == 0 )
      console.log(b)
  	if ( n % 3 == 0 && n % 5 == 0)
      console.log(f + b)
  	if ( n % 3 != 0 && n % 5 != 0)
      console.log(n)
}

Chessboard

var a = "# # # # "
var b = " # # # #"
var x = "line"

for (x = 1 ; x < 9 ; x ++ ){
  if (x % 2 == 0)
    console.log(a)
  if (x % 2 != 0)
    console.log(b)
}

Any tips on how to make my writing simpler and more concise? Thank you.

1 Like

Triangle Loop

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

FizzBuzz Loop

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

ChessBoard Loop

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

Chapter 2 Exercises

Traingle Exercise Code:

<script>

for(let hashtag = "#";hashtag.length <= 7; hashtag +="#"){
  console.log(hashtag);
}

</script>

Fizzbuzz Exercise Part 1:

Fizzbuzz Part 2:


<script>

  for (let number = 1; number <= 100; number +=1){

    if (number % 15 == 0) {
      console.log("FizzBuzz");
    }

  if (number % 3 == 0) {
    console.log("Fizz");
  }
   else if (number % 5 == 0){
    console.log("Buzz");
  }
    else {
      console.log(number);
    }
  }

</script>

Chessboard Exercise:

<script>

var size = 8;
var blank = " ";

for(var a = 0; a < size; a += 1){
  for(var b = 0; b < size; b += 1){
  if((a + b) % 2 == 0) {
    blank += " ";
  } else {
    blank += "#";
  }
}
  blank += "\n";
}

console.log(blank);

</script>

Will appreciate any comments on the work. Thanks for reading.

1 Like

Greetings @newavel, You actually got it more concise and simple compared to other students. You can actually use ternary operator to make it even more concise but that will create readability issues. Otherwise, a top notch answer from your side.

Happy Learning! :grin:

1 Like

I couldn’t make the code work on my own, had to look at the solutions, and then some other versions of solutions that people posted here. I took a good long while to really understand why and how the solutions work. However there’s no way I would have figured that out on my own having just read what was in chapter 2. For example, the use of setting empty strings! never would have thought to do that. I figured instead of posting the solutions I got from the book website and what other people posted here, I would instead ask a question - am i missing something in my approach to solving those problems? Or will it become more clear/workable as i learn more in depth?

FizzBuzz


for (var number= 1; number <= 100; number++){
    var result1 = number % 3;
    var result2 = number % 5;


    if (result1 ==0 && result2 == 0) {
      console.log("FizzBuzz");
    }else if (result1 == 0) {
      console.log("Fizz");
    } else if (result2 == 0) {
      console.log("Buzz");
    }else{
      console.log(number);
    }
  }

Chessboard

var 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);
    }
1 Like

Hi can someone explain to me why 1 and 2 will produce different answers for question number one? Your help will be greatly appreciated.

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

for(var column = 0; column < row; column++){
 pyramid += "#";     
    }
    console.log(pyramid)
  }
let x ="#"
for(row=0; row<7; row+=1){
console.log(x);
x+="#"
}

Greetings @RhoadsNRoses82, Sometimes it takes time to digest new concepts and this is very common with students. There are two ways to proceed if such a thing happens –

  1. Either you can go ahead and continue the course so that you feel more confident and familiar with the language and come back to this section and solve these questions again. Many students find this approach better.
  2. Or you could look for many examples online so that you get a feel of many approaches for the same problem and their mindset behind them.

Feel free to follow any of these. In the end, we would like to have you answer posted here so that we can evaluate your progress.

Hope this helps.

Happy Learning! :slight_smile:

1 Like

Greetings @Ziheng, The first example has two for loops in it, whereas the second example has just one. That’s why you see a discrepancy between the two results.

Happy Learning! :slight_smile:

1 Like