Chapter 2 Exercises

Still working on the exercises/assignment.I am getting interference from the editor/GoogleChrome with syntax Errors, debugger eval codes. I checked the EJ sandbox and that does work. When I follow the pattern and ver to my own, I get the GC alerts. Seems hypersensitive. Won’t give up.

Yep! Glad you figured it.

1 Like

I’m trying to do the triangle one and my code is so far:

for(height=0; height < 7; height++) {
console.log(“#”) ;
}

But in the console log it just says
(7) #

I want it to say:

.#
.#
.#
.#
.#
.#
.#

Edit: I tried to do document.write instead of console.log and I got it working

Please review the above answers in the thread to get a clear understanding of the syntax and logic.

    // Triangle Loop

    for(var result = "#"; result.length <= 7; result = result + "#"){
      console.log(result);
    }
    // FizzBuzz

    for(var number = 1; number <= 100; number++){
      if(((number%5) == 0) && ((number%3) != 0)){
        console.log("buzz");
      }
      if((number%3) == 0){
        console.log("fizz");
      }

      else{
        console.log(number);
      }
    }

I don’t think my chessboard is ideal (yet it still is working), but here it is:

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

    }

1 Like

Hello i am a bit Confused on excercise two. I thought it might work except i had a little bit of a problem with the remainder part… :neutral_face:

I have tweeked it a little and this is what I have :

for (i = 1; i<=100; i++)
{
console.log(i)
if (i % 5 == 0)
{
console.log(“Buzz”)
}

if (i % 3 == 0)
{
console.log(“Fizz”)
}
if (i % 15 == 0)
{
console.log(“FizzBuzz”)
}
}

1 Like

Looping a triangle:

for(y = "#"; y.length < 8; y += "#"){
  console.log(y);
}

Fizz Buzz:

for (x = 0; x <= 100; x++){
  let output = "";
    if (x % 3 == 0)
      output += "Fizz";
    if (x % 5 == 0)
      output += "Buzz";

  console.log (output || x);
  }

Chessboard:
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 += " ";
if ((x + y) % 2 == 1)
board += “#”;
}
board += “\n”;
}
console.log (board);

1 Like

//Looping Triangle

var num_rows = 7;
var row = 0;
var column = 0;

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

for(column = 0; column < row; column++){
toPrint += “#”;
}
console.log(toPrint);
}

//FIZZBUZZ

var f = “Fizz”;
var b = “Buzz”;
var fb = “FizzBuzz”;
for( var number = 1; number <= 100; number++){
//Print Fizz if the number is only divisible by 3
if(number % 3 == 0 && number % 5 != 0){
console.log(f)
//Print Buzz if the number is only divisible by 5
} else if( number % 5 == 0 && number % 3 != 0){
console.log(b)
//Print FizzBuzz if the number is divisible by 3 and 5
}else if(number % 5 == 0 && number % 3 == 0){
console.log(fb)
}
else {
console.log(number);
}
}

//ChessBoard

var a = 8;
//a can be any number
var b = “”;
for(var c = 0; c < a; c++){
for(var d = 0; d < a; d++){
if((c+d) % 2 == 0){
b += " ";
} else {
b += “#”;
}
}
b += “\n”;
}
console.log(b);

I had quite a hard time but i think i understand how to do it. i will keep practicing

1 Like

Excercise 1. Hash Triangle
an alternative to the solution Ivan gave for the # triangle I thought was neat, a bit more simple.

var result = “#”;
while (result.length <=7) {
console.log(result)
result += “#”
}

Excercise 2 - FizzBuzz

var i = 0
for(i=0 ; i<=100 ; i++){
if (i % 3 == 0){
console.log(“fizz”);
}
if (i % 5 == 0) {
console.log(“buzz”);
}

if ((i % 5 !== 0) && (i % 3 !== 0)){
console.log(i);
}

if ((i % 5 === 0) && (i % 3 === 0)){
console.log(“fizzbuzz”)
}

If someone can help me figure out how to remove “fizz” and “buzz” on the console line for units like 30 / 60 / 15 as it triggers all 3 to be logged (fizz/buzz/fizzbuzz) that would be much appreciated! :slight_smile:

Excercise 3 - Chess Board

var b = " "
var size = 8
for (r=0; r<=size; r++){
for (let c=0; c<size; c++){
if ((r+c)% 2 === 0) {
b += " ";
}
else {
b += “#” ;
}
}
b+= “\n”;
}
console.log(b)

While I’ve gone over this numerous times… I still have very little clue as to how the solution for the ChessBoard works… :cold_sweat: :disappointed_relieved: :cry:

Edit - this video has helped me completely understand the chessboard exercise as he goes through it step by step on an excel spread sheet… If you’re having trouble I suggest you watch this part of his explainer (Time Stamped for convenience)

Good Explainer through excel spreadsheet.

2 Likes

I jumped straight into this after doing the intro to crypto as I found it very basic, followed along quite well but got to this point and feel like I have no idea what I’m doing now. Did I make the mistake of jumping into this with no prior knowledge or am I just dumb?

1 Like

This video helped me to understand FizzBuzz
https://www.youtube.com/watch?v=QPZ0pIK_wsc

1 Like

It does get a bit overwelming… But if you just go through each line of code ( take an example from one of the answers here, and watch the video i posted right above your post… you will eventually get the hang of it… I only ever have done a free course on HTML and CSS in the past… Taking some time but getting there… I tend to watch several youtube videos on the same subject before moving on to the next chapter…

Feel it’s useful to here the same thing explained a few different ways!

2 Likes

Thanks for that actually found it quite stressful to come here with everyone nailing it and I’m here without a clue! The course would make me do the test before letting me proceed to the subject matter so I was having to work backwards too which I don’t think helped. Ivan’s # example didn’t work for me either, I went through it numerous times line for line then copied someone else’s example from the forum which worked immediately, so I think that all added to the frustration a bit. I’m going to go back and proceed through the recommended courses and re-visit with a fresh mind I think! :slight_smile:

Looping a triangle

for(row=0; row<7; row++){
	let toPrint = "#";

for(col=1; col<=row; col++){
	toPrint += "#";
}
console.log(toPrint);
 
}

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

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

1 Like

The most difficulty I’ve found was in the last question.

The FizzBuzz was somehow easy to solve since it only demand logic (at first I was forgeting about the operator && and it take me a while to figure it out that without it I would have so much work to do hahah).

I took a really long time to solve the last exercise, since I intented to solve it without looking for answers online.

It was nice to solve it. The relief eventually take me drink a cold one.

ps: Im seeing several people posting their answers, but the order was to discuss, not to answer here. Should I post my answers too?

//I am having too much fun with this triangle of “#”!!
I have removed the auto-correct on google-chrome and it still does not do what I want it to do.I have come up with 20 versions of this and it does not run a triangle. Any suggestions or corrections?

//create a triangle of “#” by looping lines (1 to 7); wherein (i = #)
var drawLineTriangle = function; (howManyLines);}
for (var i = 0; i <= 7 line.length; howManyLines; i++) {
console.log (i + “#”); drawLines;
}
};

1 Like

Marcelo,
I agree that discussion amongst those of us working on the same lesson is helpful. If you went to YouTube and listened to Benjamin, “JavaScript is Easy,” (you don’t have to have learned another language), not applicable to me. I am fluent in Spanish and studied German in high school, and JavaScript is the hardest language I have ever wanted to learn. It is beautiful and eloquent, “not easy!” Posting answers helps too!
I respect your not wanting to look at the Sandbox helps. I finally did and that was helpful. I learned more from trying other posted answers in the Google Chrome navigator. The warnings, debuggers, learn more, “what went wrong.” I am learning more from auto-correcting the proper language JavaScript, no slang.
Enjoy the process!

2 Likes

Triangle
var rows = 7;
for (var row = 0; row < rows; row++){
hash="#";
for(column = 0; column < row; column++){
hash += “#”;
}
console.log(hash);
}

FizzBuzz
for (var i=1;i<=100;i++){
text1="";
if (i%3==0) {text1=text1+“fiz”;};
if (i%5==0) {text1=text1+“buz”;};
if (i%3!=0&&i%5!=0) {text1=i;};
console.log(text1);
}

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

2 Likes

Yes please. We want all of the students taking part to post their answers too. :slight_smile:

1 Like