Chapter 2 Exercises

Looping a Triangle:
let num_rows = 7
for (let row = 0; row < num_rows; row++) {
let toPrint = “#”
for(let column = 0; column<row; column++){
toPrint += “#”;
}
console.log(toPrint);
}

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

Chessboard:
let size = 8;
let toPrint = “”;
for (let row = 0; row < size; row++) {
for (let col = 0; col < size; col++) {
if ((row + col) % 2 == 0) {
toPrint += " ";
} else {
toPrint += “#”;
}
}
toPrint += “\n”;
}
console.log(toPrint);

2 Likes

The logic that I tried and researched and plugged into atom made sense. I also plugged in the solutions to Atom, and that logic made sense also. However, I could not get the program to perform in Mozilla. I am not sure what I am doing wrong. I kept getting syntax errors both with my own code as well as the answers that were provided. Can someone tell me what I am doing in correctly. I also found I needed to set the meta charset.
Fizzbuzz

Java Script FizzBuzz program

This is a title

Chessboard

Java Script FizzBuzz program

This is a title

I reviewed the other FizzBuzz posting and it looks like if I had the
tags my code would have worked!
Looks like that was the same issue with Chessboard! The logic makes sense! I am new to programming so next time I will remember the
tags!

1 Like

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

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

Exercise 1 from the Javascript book (Chapter 2):

let toPrint = “”;
let rows = 7;
for (counter = 0; counter < rows; counter++) {
toPrint += “#”
console.log(toPrint)
};

Exercise 2 from the Javascript book (Chapter 2):

A)

let numbers = 100;
let fizz = 3, buzz = 5;
for (let count = 1; count < numbers; count++) {
if (count % fizz == 0) {
console.log(“Fizz”)
continue
}
else if (count % buzz == 0){
console.log(“Buzz”)
continue
}
console.log(count)
};

B)

let numbers = 100;
let fizz = 3, buzz = 5;
for (let count = 1; count < numbers; count++) {
if (count % fizz == 0 && count % buzz == 0) {
console.log(“FizzBuzz”)
continue
}
else if (count % fizz == 0) {
console.log(“Fizz”)
continue
}
else if (count % buzz == 0){
console.log(“Buzz”)
continue
}
console.log(count)
};

Exercise 3 from the Javascript book (Chapter 2):

“# # # #\n# # # # \n # # # #\n# # # # \n # # # #\n# # # #\n # # # #\n# # # #”;

2 Likes

The first two exercice were quite eqsy so I don’t bother you with my findings. The third was quite difficult, here is my answer (I am quite ashamed as my answer is not very elegants, but it works):
function isEven(num) {if (num % 2 === 0) {return true;} else {return false}};
var pa="";var ro=8; var co=8;
for(var y=1;y<=ro*co;y++){
if(isEven(y)) {pa+="#"} else {pa+=" “};
if(y%co==0&&!isEven(y/co)){pa+=”\n “}
else if(y%co===0&&isEven(y/co)){pa+=”\n"}
}
;console.log(pa);

1 Like

The first two exercises were easy. Not soo much the Chessboard Loop. I know its not streamline, but in the end, I just wanted to make it work. Its customizable in width and height. So here you go:

Chessboard

let line = " "
let r = 0
let height = 1 +  parseFloat(prompt("Height of Board?"))
let width = prompt("Width of Board?")

while (r <= height) {if ((line.split("#").length -1) % width == 0){
   r++; 
   if(r==height){break}; 
   if (r % 2 ==0){line += "\n#"}
   else {line += "\n "}
   }
   if (line.slice(-1) == " "){line += "#"}
   else {line += " "};
}
console.log(line)
2 Likes

question 28b

Your answer shows at the beginning of part b

var num = 0;

I’m getting error - var = 0 already declared - which makes sense but then the program doesn’t work properly.

When I remove this line then the enter a number request doesn’t appear to clear the console.log from the previous “Enter a number” and doesn’t show the correct console.log for the new number. Entering 10 ends the program which works.

var inp = prompt(“enter a number”);
let num = parseInt(inp);
counter = 0
while (counter < num) {
console.log(counter);
counter++;
}

var num = 0; //error on this line - already declared.

while (num != 10) {
    let inp = prompt("Enter a number");
    num = parseInt(inp);
}
console.log("You entered 10, the program will end now!");
2 Likes

Hi @Dougal, hope you are ok.

The problem is that you are declaring 2 variables with the same name, first you declare let num and then var num, you cant have 2 variables with the same name.

Also I dont see the point for the second while, it will go into an endless loop until inputed num is 10.

If you just keep the code this way, it works with no big error.

var inp = prompt('enter a number');
let num = parseInt(inp);
counter = 0
while (counter < num) {
console.log(counter);
counter++;
}

console.log("You entered 10, the program will end now!");

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Thank you for your help Carlos!

2 Likes

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

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

2 Likes

This is very hard to understand. I am a bit overwhelmed by all the %++=+===& and all that could someone explain al that bc it makes no sense

2 Likes

Hi @jakerichguy, hope you are ok.

Here are some links that explain in details the diferent javascript operators that can be use:

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like
  1. TRIANGLE
var stars = "";
var col = 1;
var row = 7;

for(var i = 0; i < row; i++){
    for(var j = -col; j < i; j++){
          stars += "*";
        }
     stars += "\n";
 }
console.log(stars);
  1. FIZZBUZZ
for(var i = 1; i < 100;i++){
      if((i % 3) == 0 && (i % 5) == 0){
          console.log(i + " FizzBuzz");
      }
      else if((i % 3) == 0){
          console.log(i+ " Fizz");
      }
      else if((i % 5) == 0){
         console.log(i + " Buzz");
      }
}
  1. CHESSBOARD
var size = prompt("ENTER A SIZE");
var board = "";

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

Hi @thecil & anyone else out there…

… so i’m at this stage of the course and feel like i don’t understand anything ! :man_facepalming:

I’m fine with the basic exercises but these 3 i can honestly say I had absolutely no idea, I looked at the solutions and tried to ‘back engineer’ them to understand & i’m embarrassed to say i’m still clueless…

… does anyone have a good resource (videos) to go through & perhaps i’ll then start this whole course again !

2 Likes

Hi @JDW, hope you are well.

Is it ok that this exercises can be a little bit hard for new students of programming, I could suggest to get a really good understanding on the fundamentals in order to learn quickly all further lessons on programming, also there are some concepts that the course does not go too deep on them because its “assumed” that you already know them or will learn them from the book.

But there are a loooot of good material on the internet to learn really good the fundamentals, here is a Youtube Channel that could have a lot of tutorials on the JavaScript Basics :nerd_face:

https://www.youtube.com/watch?v=2nZiB1JItbY

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

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 i= 1; i < 101; i++) {
if(i % 15 == 0) console.log( “FizzBuzz”);
else if( i % 3 == 0) console.log(“Fizz”);
else if( i %5 == 0) console.log(“Buzz”);
else console.log(i);
}

ChessBoard:
var size = 8;
var board = " ";
for (var a = 0; a<size; a++) {
for (var b = 0; b<size; b++) {
if ((a + b) % 2 == 0)
board+= " ";
else
board += “#”
}
board += “/n”;
}

1 Like

Sorry Carlo

Can you combine complete answer for 28a & b for me.
It’s not computing for me as they say. Like you said b will loop until 10 is inputted. So how is 28b meshed with 28a to get a prompt for numbers 1 to 9 but when it gets to 10 it exits, because now they are two separate entities.

28a
var inp = prompt(‘enter a number’);
let num = parseInt(inp);
counter = 0
while (counter < num) {
console.log(counter);
counter++;
}

28b
var num = 0;
while (num != 10) {
let inp = prompt(“Enter a number”);
num = parseInt(inp);
}
console.log(" you entered 10, the program will end now");

1 Like

Sure, I think all you need is an if conditional that does not run the loop if the condition of 10 is reached.

var inp = prompt("enter a number");
let num = parseInt(inp);
counter = 0;
if (num == 10) {
  console.log("You entered 10, the program will end now!");
} else {
  while (counter < num) {
    console.log(counter);
    counter++;
  }
}

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Tks, yes that works. The answer in the course materials had While instead of If which kinda threw me off.

// Looping Traingle

var str = “#”;
var rowMax=7;

for (i=0; i<rowMax; i++){
console.log(str+"\n");
str +="#";
}

// FuzzBuzz

var numMax =15;

for ( i=1; i<numMax+1; i++){

var msg=""; // reset text string
if ((i%3)==0) msg +=“Fizz”; // exactly divisible by 3
if ((i%5)==0) msg +=“Buzz”; // exactly divisible by 5

console.log(msg||i);
}

// Chess Board.

var chessPattern= “”;
var pHeight = 8;
var pWidth =8;

for( i=0; i<pWidth/2; i++){ // build line pattern * * * *
chessPattern +="* ";
}

for( i=0; i<pHeight; i++) { // build board

if (i%2==0) {console.log( " "+ chessPattern);} // odd line

else {console.log(chessPattern);} // even line

}

1 Like