Chapter 2 Exercises

It is assigned has an string value, when you use "" on a variable, you are basically assigning it a blank string message, but it is still count has an string variable type.

Carlos Z

1 Like

The Chessboard exercise took me longer than expected.
However, here is my code for comparison:

//Book Exercise Page 38 oneSQssboard
//November 06,2020

//Write a program that creates a strFinaling that represents an 8x8 sizeGrid,
//using newline characters to seperate lines. At each position of The
//sizeGrid there is either a space or a #character =. The characters should
//form a oneSQssboard. Passing this strFinaling to a console.log;

console.clear();

var sizeGrid = 10;
var oneSQ = " ";
var strFinal = “”;
var nextLine = “\n”;

console.log(“Chessboard”+nextLine);

//The “outer” loop controls rows

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

//The inner loop controls the columns

for (var i = 1; i < sizeGrid +1 ; i++) {

//Add a square to the string, representing a column

strFinal = strFinal + oneSQ;

//Perform the swith of  #, to " " or viceversa, accordingly for Chessboard

if (oneSQ == " ") {
  oneSQ = "#";
} else if (oneSQ == "#") {
  oneSQ = " ";
}

}

//Add the break line at the end

strFinal = strFinal + nextLine;

//Perform again the swith of #, to " " or viceversa, accordingly for Chessboard
//to start new row with different square type.

if (oneSQ == " ") {
oneSQ = “#”;
} else if (oneSQ == “#”) {
oneSQ = " ";
}
}

//Print the final string.

console.log(strFinal);

1 Like

First exercise:
var hash = “#”
while(hash.length < 8){
console.log(hash)
hash += “#”
}

Second exercise:var number = 1
while(number <= 100){
if(number % 15 == 0){
console.log(“FizzBuzz”);
number++}
else if(number % 3 == 0){
console.log(“Fizz”);
number++;
}
else if(number % 5 == 0){
console.log(“Buzz”);
number++;
} else {console.log(number) ;
number++}
}

Third exercise:
var firstLine = "# “;
var secondLine = " #”;

for(var counter = 0; counter<3; counter++){
firstLine += “# "
}
for(var counter1 = 0; counter1 <3;counter1++){
secondLine += " #”
}
var image = firstLine + “\n” + secondLine + “\n”;
for(var counter2 = 0; counter2<3; counter2++){
image += firstLine + “\n” + secondLine + “\n”;
}
console.log(image)

1 Like

Fizz Buzz

for (i=1;i<101;i++){
if (i%3==0 && i%5==0) {console.log(“FIZZBUZZ”)}
else if (i%5==0) {console.log(“BUZZ”);}
else if (i%3==0) {console.log(“FIZZ”); }
else{console.log(i);}
}

Chess board

let num=prompt(“How many rows and colums in your board?”);
let board="";
for(let x=0;x<num;x++){
for(let i=0;i<num;i++) {
if ((i+x)%2 == 0){
board += " ";
} else {
board += “#”;
}}
board += “\n”;
}
console.log(board);

1 Like

I don’t really understand why the author of the book has made such a complex solution for the triangle. We had it already running a few lessons ago and that solution by the academy does make a lot more sense to me, to be honest.

Nontheless: The first two exercises were OK to solve for me, the third gave me headache. I didn’t even know where or how to start. I read the complete chapter again and it didn’t help me. But that should more be like a logical blockade than not knowing how to write the code.

Anybody got a hint how to think outside the box a bit more?

(At this point I have to say that I am so happy with the academy because of the progress going slowly but surely - it is perfect for me!)

This is usually common at the begining sir, by experience, the only way to start thinking out of the box on programming, is by practicing, keep the knowledge fresh in your head, then any problem that you could face, you can figure it out some kind of solution, but only by practicing a lot to learn some few tricks.

Although the exercises could looks complex, their good practice to start getting into that mindset “think out of the box” because you are forced to think by yourself in a possible solution, and it will be the same feeling once you start programming something by your own.

Carlos Z

2 Likes

for (a=0;a<120;a++)

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

if(a%3!=0 && a%5==0){console.log(“buzz”)}
else
if(a%3==0 && a%5!=0) {console.log(“fizz”)}
else {console.log(a)}

1 Like

//-----------------------------------[Triangle]
pound = “#”; count = 1;
while (count <= 7){
console.log(pound);
pound = pound + “#”;
count++;
}
//-----------------------------------[FizzBuzz]
for(i=1;i<100;i++)
console.log(
( ++i%3 ? ‘’ : ‘fizz’ ) + ( i%5 ? ‘’ : ‘buzz’ ) || i
)
//-----------------------------------[Chess Board]
size = 8;
board = “”;
for( i = 0; i < size; i++){
for( j = 0; j < size; j++){
board += (j % 2) == (i % 2) ? " " : “#”;
}
board += “\n”;
}
console.log(board);

1 Like

for the fizzbuzz exercise i wrote the following code. it appeared to accomplish the desired outcome. however it is a bit longer than the solution that the book gives. i was quite proud of myself, but now am questioning myself. is my logic on the correct path? or am i missing some key element that would allow me to shorten the code?
why did they put “let var n=1” in the “for” parentheses instead of just “n=1”?
in their code if the number was divisible by both numbers then the console.log would display both fizz and buzz?
i dont see a plus sign anywhere to concatenate the outputs?
if you put two “if” functions back to back does that automatically concatenate them if they are both true?
would appreciate some feedback on my thought process and why my code was longer.

i=0;
for(;i<101;i++){
if (i%3==0 && i%5==0)
{
console.log(“fizzbuzz”);
}
else if (i%5==0)
{
console.log(“buzz”);
}
else if (i%3==0)
{
console.log(“fizz”);
}
else{(console.log(i))}
}

1 Like

Hey @Regis_Brickett, hope you are good.

Now your code is almost perfect, 2 little mistakes:
i=0, you are not defining this has a variable, must be var i = 0 or let i = 0.
You have an extra ; at the beginning of your for loop.

Suggestion: you can just define i inside the for loop, because i will just be used to count the iterations of your for loop. So it can be for(let i = 0; i < 101; i++)

Because is not need it, with the console.log(), you are just printing into the console a word (string variable), which is “fizzbuzz”, thats why you do not need a plus sign or nothing to concatenate, because you are not using variables, just a showing a word.

I do not understand quite well this question, you mean something like?

if(condition){
//do this
}
if(condition){
//do this
}  

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

Carlos Z.

to reply to your last point what i mean is

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

you see how they have the two “if” statements back to back? i thought that would print the words on two different lines.

like:

fizz
buzz

1 Like

for (let line = “#”; line.length < 8; line += “#”)
console.log(line);
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);
}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

chess board
i racked my brain for a while and googled things for a while before i gave up.i came up with a clunky, but effective code to print an 8x8 board. however, my code was not able to accommodate different board dimensions. i looked at the solution to reverse engineer it. i understand it for the most part. the first loop executes until the second loop breaks. when the second loop breaks and the first loop moves onto its second iteration does that reset the second loop initialization back to zero?

1 Like

//CHAPTER 2 EXERCISES
//EXERCISE 1 Looping Triangle
var hash =’’;
var i = 0;

while(i<7){
hash = hash + ‘#’;
console.log(hash);
i++;
}

// FIZZ BUZZ
for(var i = 1; i<101; i++){
//console.log(current);
if (i % 15 == 0) console.log(‘FIZZ BUZZ’);
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 gridOutput = ‘’;
var rowOutput = ‘’;

var gridSpace = ’ ';
var gridHash = ‘#’;

//outer loop
for (var row = 1; row <= size; row++ ) {

//start w/ space
if( row %2 != 0 ) {
var currItem = gridSpace;
var nextItem = gridHash;
}

// start w/ hash
else {
var nextItem = gridHash;
var currItem = gridSpace;
}

rowOutput = ‘’;

for ( var column = 1; column <= size; column++ ){

// build the row by adding each column
if( column%2 == 0 )
rowOutput += currItem;
else
rowOutput += nextItem;

}

gridOutput = gridOutput + rowOutput + ‘\n’;

}
console.log( gridOutput );

1 Like

1 -looping a triangle

for(var a = 0 ; a < 7 ; a++){

var print = “#”

for(var i = 0 ; i < a ; i++){

print += “#”

}

console.log(print)

}

2-fizzbuzz

for( var a = 0 ; a < 100 ; a++){

var output = “” ;

if(a % 3 == 0) output += “fizz”;

if(a % 5 == 0) output += “buzz”;

console.log(output || a)

}

3- chessboard

for(var a = 0 ; a < 4 ; a++){

var b = " # # # #";

var c = "# # # # ";

console.log(b)

console.log( c )

}

2 Likes

If will not print 2 times the conditions, becuase their inside of a loop, meaning there is 1 value per iteration, so he will evaluate the value on those 2 conditions, if one of those is true, it will return the condition logic (which is the output adding).

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

Carlos Z.

Yes, it reset the 2nd loop to zero, because its inside the 1st one, meaning for each iteration of the 1st one, it will run all the logic from zero, including the 2nd loop.

Carlos Z

I feel your pain dude.

1 Like

I kinda cheated. I found the solution for the chess problem first. I took what I learned from that and used it to do the fizz Buzz and piramid ones. I did those ones by myself. No way I would have been able to complete this without a head start. I get the logic but my punctuation is really poor. It’s like learning a new language :joy: I’m getting there, slowly.

2 Likes

okay these are my solutions, theres better solutions here, but these are my solutions, i didnt cheat on any of them so thats good i guess.

Triangle loop

var star = “”;
var line = 0;
while(line<8){console.log(star); star=star+"#"; line++;}

this way is crappy but ialso wrote and understood ivans way , and then from what i learned there i did the other exercises.

Fizzbuzz exercise

for(var numero = 0; numero<101; numero++)

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

else if(numero%3==0){console.log(“fizz”);}

else if(numero%5==0){console.log(“buzz”);}

else{console.log(numero);}}

Chessboard exercise

var user = prompt(“write a number:”);

var white = " #";

var black = "# ";

for(var numero = 0; numero<user; numero++){

if(numero%2==0){console.log(white.repeat(user));}

else{console.log(black.repeat(user));}

}

the last one is kind of crappy too, but im getting there xp

2 Likes