Chapter 2 Exercises

I like ur approach Gos

1 Like

Looping a Triangle

function makeLine(length){
var line = “”;
for (var j = 1; j <= length; j++){
line += "# ";
}
return line + “\n”;
}
function buildTriangle(x){
var triangle = ‘’;
for (t = 1; t <= x; t++){
triangle += makeLine(t);
}
return triangle;
}
console.log(buildTriangle(7));

FizzBuzz
(I did a bit extra and added some HTML and a style sheet)

function FizzBuzzA(value1, value2){
let returnValue = “”;
for(let i = 1;i<=100;i++){
if(i%value1==0 && i%value2==0){
returnValue += 'FizzBuzz ';
}
else if (i%value1 == 0){
returnValue += 'Fizz ';
}
else if (i%value2 == 0){
returnValue += 'Buzz ';
}
else{
returnValue += i + ’ ';
}
}
return returnValue;
}

(I haven’t included the index.html code and style.css)

Chessboard

// create a size variable, set = to 8
let size = 8;
// create an empty string results
let result = ’ ';
// loop to create rows
let row = 1;
while (row <= size){
// loop to create columns
let column = 1;
while (column <= size){
// if column plus row is even
if((column + row) % 2 === 0){
// add and empty space
result += " ";
//else
} else {
// and an octothorpe #
result += “#”;
}
column += 1;
}
// add a newline symbol to end current
result += ‘\n’;
row += 1;
}
//log string to the console
console.log(result);

1 Like

Looping Triangles
var number = 7;
for (var count = 1; count<=rows; count++){
var toPrint = “”;
var i=0; while (i<count) { toPrint += “#”; i++;
}
console.log (toPrint);

Fizz Buzz solution:

for (var a =1; a<101; a++){
var output="";
if(a%3==0){output +=“fizzz”;}
if(a%5==0){output +=“buzzz”;}
console.log(output||a);
}

Chessboard Solution

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

1 Like

My characters have changed colors. My + = signs were blue and now they are purple. Since this change, none of the programs seem to be running correct. I’m to the exercises in chapter 2, and can not get anything to run.

1 Like

Triangle Loop
var row_num = 7;
for ( row = 0; row < row_num; row++){
var ToPrint = “#”;
for ( colum = 0; colum < row; colum ++){
ToPrint += “#”

}

console.log(ToPrint);

}

FizzBuzz Loop

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

} else if(counter % 5 == 0){
  console.log("Fizz");

}else{
  console.log(counter);
  

}

}

ChessBoard

let size = 8;
let board = “”;
for(var row_horizon = 0; row_horizon< size; row_horizon++){
for(var colum_ver = 0; colum_ver< size; colum_ver++){
if((row_horizon + colum_ver) % 2 == 0){
board = board + " ";
} else {

    board = board + "#";
  }
}
board = board + "\n";

}

console.log(board);

1 Like

Hey @CryptoCowboy1311, hope you are well.

Please share your code in the following way so I can replicate the issue and help you solve it :nerd_face:

Carlos Z

Is there anybody that could help with my issue below, please?:

I managed to get the “chessBoard” exercise done using the next code:

var size=prompt("Please insert a grid size")

        for(row=0; row<size; row++){
          if(row%2==0){for(column=0; column<size; column++){
            if(column%2==0){c="#"} else{
              c="0";
            }
            document.write(c);
          }; document.write("<br>")} else
                  {for(column=0; column<size; column++){
                        if(column%2==0){d="0"} else{
                          d="#";
                        }
                        document.write(d);
                      }document.write("<br>")}
      }

Issues:

  • Why can I get a “chessboard”, if I’m using “#” & “0”, but if I am using the “#” and " ", the first and last space gets ignored ?

  • How should I correctly use the /n ? (I tried and all I get is a space, but not a new line…)

This seems to be working correctly for me. :thinking:

Please use the \n instead. This will create a new line successfully.

Hope this helps. :slight_smile:

1 Like

I tried “\n” too… But all I get is a space… Strange … Or I must be doing something terribly wrong :))

Thanks for your feedback Malik ! Much appreciated!

1 Like

1.for (let line = “#”; line.length < 8; line += “#”)
console.log(line);

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

3.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 Likes

Hi,
This was really tough. It took about 3 hours of thinking before I gave up. I was able to get some idea of how to code. I really did not want to copy the code without truly understanding it. What bothers me a little is the various ways to code. It is a bit daunting since I just started, but I am do not want to give up. I will continue to push through this challenge. Here are my answers:

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 (a=1; a<=100; a++){
let output = “”;
if(a%3==0) output += “Fizz”;
if(a%5==0) output += “Buzz”;
console.log(output||a);}

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

3 Likes

Courage. Your solutions are great. remember, Never Give up!

1 Like

Thank you so much. I appreciate your words of encouragement.

Looping Triangle

var row = "0";
var mrow = "7";
var Print = "#";
while(row<mrow){
        console.log(Print);
        Print+="#";
        row++;
        }
1 Like

FizzBuzz- part 1

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

1 Like

FizzBuzz part 2

  for (var num = 1; num <= 100; num++) {
    if (num % 3 === 0 && num % 5 === 0){
      console.log("FizzBuzz")
    } else if (num % 3 === 0) {
      console.log("Fizz")
    } else if (num % 5 === 0) {
      console.log("Buzz")
    }else {
    console.log(num);
    }
  }
1 Like

let numTriangle = 7;
for(let triangle = “#”; triangle.length <= 7; triangle +="#")
console.log(triangle);Preformatted text

Not sure what ‘Preformatted text’ means. After many tries with more complicated coding, I found this to be the simplest of all the triangle codes. I tried a previous code in the JS sandbox and it printed 3 triangles, 1 to 7 strings. I only needed one to print. So I reduced the 3 to find the simpliest and it matched one that I found on YouTube. The goal was to produce a hashtag triangle. This one does not have curly brackets, or “stringToPrint” etc. My first had a 2 part with let i = 1 and later let N = 1. The hint in JS + = “#” was useful. This printed in the sandbox, but not here.

1 Like

Looping a Triangle

var hash = “”;
var counter = 0;
while (counter < 7) {
hash = hash + “#”;
counter = counter + 1;
console.log(hash)
}

1 Like

/* “Fizz Buzz”
when both 3% && 5% = zero between 1 to 100 examples are:
15, 30, 45, 60, 75, 90*/
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);
}
}

1 Like

/* “Fizz Buzz”

when both 3% && 5% = zero between 1 to 100 examples are:
 15, 30, 45, 60, 75, 90*/
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);
    }
 }

// I had an extra curly bracket, once removed, it runs correctly. Must review, save, refresh!

2 Likes