Chapter 2 Exercises

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

1 Like

OK :ok_hand: Sorry about that.

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

Checkerboard

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

thank you. i will change it

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

Triangle:

in while loop:

<script>
    var row = 0;
    var hash = "#"

    while (row < 7) {

      console.log(hash);
      row++;
      hash = hash + "#"
    }
  </script>

OR in a for loop:

<script>
    for (var row = "#"; row.length < 8; row += "#") {

      console.log(row);

    }
  </script>

FizzBuzz Solution:
I printed +row so you can check…

<script>
    for (var row = 1; row <= 100; row++) {
      if (row % 3 == 0 && row % 5 == 0) {
        console.log("FizzBuzz" + row);
      } else if (row % 3 == 0) {
        console.log("Fizz" + row);
      } else if (row % 5 == 0) {
        console.log("Buzz" + row);
      }
    }
  </script>
<script>
    var size = 8;
    var row = 1;
    var column = 1;
    var hash = "# ";

    while (row <= size && column <= size) {
      document.write(hash.repeat(size));
      document.write("<br>");
      row++;
      column++;
    }
  </script>
1 Like

Hello,

I do not understand how to do either the FizzBuzz exercise or the Chessboard exercise. Can you please help me out here? How am I supposed to set them up?

There is no way I will be able to figure this out all on my own. I barely understood a majority of the material on loops.

I have always hated word problems in math when I was in school. This is even worse.

Thank you!

Solution on Chapter 2 exercises:
Looping a triangle:
for (let triangle="#"; triangle.length<8; triangle=triangle+"#"){
document.write (triangle)}
FizzBuzz
let printing1=“Fizz”;
let printing2=“Buzz”;
let printing3=“FizzBuzz”;
for (let count=1; count<100; count++){
if (count%3===0)console.log(printing1);
if (count%5===0)console.log(printing2);
if (count%3===0 && count%5===0)console.log(printing3);
else console.log (count);
}
ChessBoard
let board="";
for (let i=0; i<8; i++){
for (let c=0; c<8; c++)
if ((i+c)%2===0) board=board+" “;
else board=board+”#";
board=board+"\n";}

Hi @jgentry sometimes things seems difficult than they are.
Lets see the first one,

Write a program that uses console.log to print all the numbers from 1 to 100,
with two exceptions. For numbers divisible by 3, print “Fizz” instead of the
number, and for numbers divisible by 5 (and not 3), print “Buzz” instead.
When you have that working, modify your program to print “FizzBuzz” for
numbers that are divisible by both 3 and 5 (and still print “Fizz” or “Buzz”
for numbers divisible by only one of those).
(This is actually an interview question that has been claimed to weed out
a significant percentage of programmer candidates. So if you solved it, your
labor market value just went up.)

Given
1, Write a program that uses console.log to print all the numbers from 1 to 100(in this sentence, we know that we are gonna need a loop in out function) for(){} and a variable that represent a current number.
2, For numbers divisible by 3, print “Fizz” instead of the
number,(here we have to check if the number divisible by 3, so we need to think of mathematical operation that can be used, a condition and a function for printing the “fizz” ) things you need to know:- % , condition(if , else if or else) and console.log().
3, for numbers divisible by 5 (and not 3), print “Buzz” instead.(same as number 2 just change 3 by 5 )
4, When you have that working, modify your program to print “FizzBuzz” for
numbers that are divisible by both 3 and 5 (you have check to both conditions in one condition, so you have to think of a logical operation like AND or OR) things you need to know:- %, condition(if, else if or else), console.log() and logical operation.
5, (and still print “Fizz” or “Buzz”
for numbers divisible by only one of those). this is the tricky part, here you need to think of which condition you should check first. is it 2, 3 or 4?

I hope this makes a little bit sense :grinning: let me know if it helps then we do the same for the third question.

Happy coding,
Abel

1 Like

Hi @Alexandr_Els could you check post your answer as code format?please take a look at picture below.

I did think that a for loop would be necessary; however, I am having trouble figuring out what to put in the parentheses or the curly brackets. Also, the whole ordeal with the if, if-else, etc. is still difficult for me to understand. Some of this very complex material just seems to go right over my head.

I kept referring to the Eloquent Javascript book, but it seemed to help only a little.

Thank you,
Joanna Gentry

I still barely understand.

  1. What does that current number need to be?

  2. What needs to go in the parentheses following the console.log?

I am very frustrated right now. Please help me. I cannot move forward until this is done.

Thank you!

1 Like

Hi @jgentry No worries, I understand your frustration. I went through same process first time I tried to learn programming. it is a very common experience. :smiley:

1, current number is the current number in the loop for(var i = 1; 1 <= 100; i++){
console.log(i);
}
in first loop current number is 1 then second loop will be 2 because it increments by 1 so it goes in that order till 100.
check this link it will help a lot about loop https://www.w3schools.com/js/js_loop_for.asp,
2, console.log() is a built in function. it prints whatever you pass in parentheses. console.log(“Fizz”) or console.log(“Bizz”).

1 Like

Okay. Maybe this simpler strategy will help.

I keep looking at other students’ answers for advice, but that is a temptation that needs to be avoided. Cheating is totally unacceptable. I need to figure this stuff out on my own because I might have to do it like this one day.

Thank you.

1 Like

I also realized it would be better to perform the exercises in the console of my browser rather than in Atom.

Once I feel that I have the FizzBuzz exercise down, I’ll try the same strategy for the Chessboard exercise. But if I need extra help, I’ll get it.

Again, thanks a lot!

1 Like

Hi @jgentry take a look at this site https://www.w3schools.com/js/js_loop_for.asp

1 Like

I got your previous reply with the article, but I haven’t looked at it yet. Thank you!

1 Like

Don’t you have to break the function at certain points?

no, you do not have to break it

1 Like

The information in my console looks something like this:

for(var i=1; 1<=100; i++){
    
}

What needs to go in these curly braces? I’m trying to get as much help as possible without copying other students’ answers or cheating.

Thanks!

you need to try to implement the three condition in the curly braces.

1 Like