Chapter 2 Exercises

unless I am mistaken, divide 3 by 3 is 1 but there is 0 left so the value you keep for this expression is 0, so if you divide 4 by 3, the answer is also 1 but you have 1 left, which would mean the expression will deliver 1 or if you divide 5 by 3 you get 1 but you have 2 left (because 5 - (1 x 3) = 2) so the expression will deliver 2, hope this helps. % is the remainder operator, so you end up with what is left after you have divided what you can (without resulting in fractions).

  1. `
for (let line = "#"; line.length < 8; line += "#")
  document.write(line + "<br>");
  1. I know that exercise says to use console.log but I couldn’t make it with it, it just didn’t appear anything when I tried to use console.log so that’s what I done:
for (var number=1; number <= 100; number+=1){
          if (number % 3 ==0 && number % 5 == 0 ) document.write("FizzBuzz <br>");
          else if (number % 3 == 0) document.write("Fizz <br>");
          else if (number % 5 == 0) document.write("Buzz <br>");
          else document.write(number + "<br>");
        } ;
  1. grr after three days, still not working for me, even the book solution doesn’t come up like it suppose to. can someone tell me why after I put <\n> in code below, it doesn’t make new line but something like this: # # # #< ># # # # < > # # # #< ># # # # < > # # # #< ># # # # < > # # # #< ># # # # < >
    ` ```
    let size = 8
    let line = " #"
    let bline = "# "
    for(x=0; x< size / 2; x++){document.write(line.repeat(size/2) + “<\n>” + “”+ bline.repeat(size/2) + “<\n>”) };

Looping Excerises

While

let triangle = “#”;
while (triangle.length <= 7) {
console.log(triangle);
triangle +="#";
}

For

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 exercise

Let f = 1;
While (f <= 100) {
if (f % 15 == 0) console.log(“FizzBuzz”);
else if (f % 5 == 0) console.log(“Buzz”);
else if (f % 3 == 0) console.log(“Fizz”);
else console.log(f);
f = f + 1;
}

Chessboard excercise

Let size = 4;
Let board = “”;

for (x=0; x<size; x++) {
for (y=0; y<size; y++) {
if ((x+y)% 2==0) {
board += “ “;
}
else {
board += “#”;
}
}
board += “\n”;
}
console.log(board);

1 Like

On let hashtag = “#”;
while(hashtag.length<7>){
console.log(hashtag);
hashtag = hashtag + “#”;
}
console.log(hashtag);

for(var i=1; i<=100; i++) {
// If the number we are looking at (loop variable “i”) is divisible by 3 AND 5
// examples - 15, 30, 45,60, 75, 90

else if( i%3 == 0) {
console.log(“Fizz”);
}
// 3 * 33 = 99 is less than 100

else if( i%5 == 0) {
console.log(“Buzz”);
}
// 5 * 20 = 100

If( i%3 == 0 && i%5 == 0) {
console.log(“FizzBuzz”);
}
else {
console.log(i);

}
}

1 Like
  1. Looping a Triangle
var rows = 0;
var columns = "#";
while(rows < 7){
    for(counter = 0; counter < 7; counter++){
        console.log(columns);
        columns+="#";
        rows++;
    }
}
  1. FizzBuzz
var count = 0;
while(count <= 100){
    if(count == 0){
        count++;
    }else if(count%3 == 0 && count%5 != 0){
        console.log("Fizz");
        count++;
    }else if(count%3 != 0 && count%5 == 0){
        console.log("Buzz");
        count++;
    }else if(count%3 == 0 && count%5 == 0){
        console.log("FizzBuzz");
        count++;
    }else{
        console.log(count);
        count++;
    }
}
  1. Chessboard
var binding = 8;
var rowodd = "";
var roweven = "";
for(i=0;i<binding;i++){
    if(i%2==0){
        rowodd+=" ";
        roweven+="#";
    }else{
        rowodd+="#";
        roweven+=" ";
    }
}
for(i=0;i<binding;i++){
    if(i%2==0){
        console.log(rowodd);
    }else{
        console.log(roweven);
    }
}
1 Like
  1. Looping a triangle:
    var output = " ";
    var size = 7;
    for(var row = 1; row <= size; row++){
    for(var column = 1; column <= size; column++){
    if(column < row)
    console.log(output += “*”);
    }
    console.log(output += “\n”);
    }

  2. Fizz Buzz:
    for(var i = 1;i <= 100; i++){
    var Output= “”;
    if((i % 3 == 0) && (i % 5 == 0)) {Output += “Fizz Buzz”}
    if((i % 3 == 0) && (i % 5 != 0)) {Output += “Fizz”}
    if((i % 5 == 0) && (i % 3 != 0)) {Output += “Buzz”}
    if(Output == “”) {Output = i}
    console.log(Output);
    }

  3. Chess Board:
    var size = 8;
    var Output = " ";
    for(var row = 1; row <=8; row++){
    while (row <= size){
    var column = 1;
    while (column <= size){
    if((column + row) % 2 == 0){
    Output += " ";
    } else{
    Output += “#”;
    }
    column += 1;
    }
    Output += “\n”;
    row += 1;
    }
    }
    console.log(Output);

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 += “#”;
}
console.log(toPrint);
}

fizzbuzz loop

for (var num = 1; num <=100; num++){
var output = num;

if (num % 3 === 0 ) {
output = “fizz” ;
}
if (num % 5 === 0 ){
output = “buzz”;
}
if (num % 3 === 0 && num % 5 ===0){
output = “fizzbuzz”;
}
console.log(output);

}

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

My versions are longer than the answers, still I managed to get the outputs right:

1:
var a = “#”;
while(a.length < 8) {
console.log(a);
// console.log("
");
a = a + “#”;
}

2:
for (var b = 1; b < 101; b = b + 1) {
if (b % 3 == 0 && b % 5 == 0) {
console.log(“Fizzbuzz”);
}
else if (b % 3 == 0) {
console.log(“Fizz”);
}
else if (b % 5 == 0) {
console.log(“Buzz”);
}
else {
console.log(b);
}
}

3:
var size = 8;
var x = “\xa0”;
var y = “#”;

for (var row = 0; row < size; row = row + 1) {
if (row % 2) {
var str = “”;
for (var a = 0; a < size; a = a + 1) {
if (a % 2) {
str = str + x;
}
else {
str = str + y;
}
}
}
else {
var str = “”;
for (var b = 0; b < size; b = b + 1)
if (b % 2) {
str = str + y;
}
else {
str = str + x;
}
}
console.log(str)
}

javedkhalil,
I am intrigued with your coding for FizzBuzz considering the number 15 in the initial set up since 3 and 5 come together at number 15 (3 x 5 =15), and is less than 100. And also, 30, 45, 60, 75, 90, and not past 100 wherein the loop would stop since 105 is greater than 100. Since these numbers were not presented in the initial discussion of the assignment, I wondered if it would be confusing to follow. Using the information given was clearer for me. It took me awhile because I had noticed % 15 in more than 1 answer by others. I haven’t worked with it yet. Does the code work when you reduce 100 to 10? (both 3 and 5 are < 10 and meet the criteria of <= 100. It might work as well? I look forward to trying your FizzBuzz Loop for fun. It is fascinating how this coding works. Thank you,
Lorraine

for(var 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);

  

}

}Preformatted text

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

FizzBuzz

for(let n = 1; n<=100; n++){
let output = “”
if (n%3==0) output = “Fizz”;
if (n%5==0) output = “Buzz”;
if (n%3==0 && n%5 ==0) output = “FizzBuzz”
else {console.log (n);}

Chessboard

let size = 8
let grid = “”
for (let x = 0; x<size; x++){

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 +="#";
      }
      console.log(toPrint);
  }

Fizzbuzz

for(var j = 1; j <= 100; j ++){

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

Chess Board Loop

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

      if(row % 2 || 0){
          toPrint = " # # # #";
      } else {
          toPrint = "# # # #";
      }
      console.log(toPrint);
  }
2 Likes
for (let number = "#"; number.length <= 7; number = number +"#") {
  console.log(number);
}
for (let buzzer = 1; buzzer <= 100 ; buzzer = buzzer + 1) {

if (buzzer % 3 == 0 && buzzer % 5 == 0)
{console.log('FiZzBuZz');} 

else if (buzzer % 3  == 0) {
   console.log('FIZZ');}

else if (buzzer % 5  == 0) {
console.log('BUZZ');} 

else {console.log(buzzer);}}
let field = '';
let size = 8;

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

for(let y=0; y<=size; y++) {

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

{field+= ' ' ;

}

else {

field+= '#' }

}

field += '\n';

}
1 Like

Now it should it be correct. Didn’t know you can part them with the three ` thanks for that @Malik

1 Like

I wrote my code this way, and it differs from the textbook answer even thou the results are the same. Is there anything wrong with this?

for (a=1; a <=100; a++){

  if(a%3 ==0 && a%5 !==0){
    console.log("Fizz");
  }
  else if(a%5 ==0 && a%3 !==0){
    console.log("Buzz");
  }
  else if(a%3==0 && a%5==0){
    console.log("FizzBuzz");
  }
  else console.log(a);

}


/* Textbook solution

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

*/

3 Likes

This answer is fine as well.

Happy Learning! :slight_smile:

1 Like

Great Exercises!
Please let me know what you think about my approach.
Don’t see anyone else using this approach but it works.

Here is my code:

    <script>

    //Triangle
    var rowLength = 7;
    for(row = 0; row < rowLength; row++){
      var toPrint = "#";
      for(column = 0; column<row; column++) {
        toPrint += "#";
      }
      console.log(toPrint);
    }
//FIZZBUZZ
      for(let counter = 1; counter <= 100; counter++){

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

        if(counter % 3 === 0 && counter % 5 !== 0){
          console.log("fizz");
        }
        if(counter % 5 === 0 && counter % 3 !== 0){
          console.log("buzz");
        }

          else if(counter % 5 !== 0 && counter % 3 !== 0){

            console.log(counter);
}

      }

      //chessboard
      var rows = 8;
//Change var rows to change row length chessboard
      for(row = 0; row < rows; row++){
        if(row % 2 === 0){
          var multiStr = "# ".repeat(8/2);
//Change First number after repeat to change column length chessboard(part1/2)
           console.log(multiStr);
        }
        else if(row % 2 !== 0){
          var multiStrTwo = " #".repeat(8/2);
//Change First number after repeat to change column length chessboard(part2/2)
            console.log(multiStrTwo);
        }

      }


    </script>
1 Like

Looping triangle:
for(var count = 0; count<7; count = count + +){
var toPrint = “#”;
}
for(var counter = 0; counter<7; counter = counter++){
toPrint += “#”;
}
console.log(toPrint);
}

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

Chessboard:
for(var count=1;count<9;count++){
if(count%2=1)console.log(" ####");
else console.log("####")
}

1 Like