Chapter 2 Exercises

This one took a bit longer, needed to make sure I understand why everything is where it is. I will work on this with other variable names and other loop options just to get clearer with it.

  let size = 8;
  let square = '';
  let row = 1;

  while(row <= size) {
    let column = 1;

  while(column <= size) {
    if ((column + row) % 2 ==0){
      square += " ";
  }
    else { square += "#";
  }
  column += 1;
  }
  square += '\n';
  row += 1;
  }

  console.log(square);
1 Like

Fizzbuzz:
image
Looping triangle:
image
Chess board:
image
This one I consulted many videos and read many blogs, but I still couldn’t do it. I tried to apply the triangle exercise findings to it but it derailed me further. This is the result after watching few videos.

//Triangle

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

//FizzBuzz

  for(var i=1; i<=100 ; i++){
    var write="";
    if (i%3==0){
      write="Fizz";
    }
    if (i%5==0){
      write+="Buzz";
    }
    if (write!=""){
      console.log(write);
    }
    else{
      console.log(i);
    }
  }

//chessboard Had some fun with this one, can be used on any given size of chessboard (including chessboard with uneven size number)

var size=parseInt(prompt("Give a number"));
  var line="";
  var newline=0;
  for (var i=1; i<=(size*size); i++){
    if ((i+newline)%2==0){
      line +="#";
    }
    else{
      line+=" ";
    }
    if (i%size==0){
      line+="\n";
      if (size%2==0){
        newline++;
      }
    }
  }
  console.log(line);
1 Like

Fizzbuzz

for (var i=1; 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);
}

@PavlinT
The question mark is a shorter way of writing an if else command.
(if condition is true) ? (then do this) : (else do this)

board += (a % 2) == (i % 2) ? " " : “#”;
is the same as:

if ((a % 2) == (i % 2) ) {
  board+=" " }
else  {
 board +="#"}
3 Likes

It wasn’t a vey clean code, but I am glad I got the result!

/* Looping a triangle */

var toPrint = “#”

for(i=0;i<7;i++){

    console.log(toPrint)

    toPrint += "#"

}

/* FizzBuzz */

var item = 0

for (number=1;number<101; number++){

if (number%3==0 && number%5==0) item = "FizzBuzz"



else if(number%3==0) item = "Fizz"



else if (number%5==0) item = "Buzz"



else item = number



console.log (item)    

}

/* Chessboard */

const size: 8

var boolian = true

var sequence = " "

for (column =0; column<size; column++){

if (column%2==0){

    sequence = " "

    boolian = true

    }

    else{

    sequence = "#"

    boolian = false}

for (row=0; row<size-1; row++){

    if (boolian ==true )

     {   sequence += "#"

         boolian = false}

    else {

        sequence += " "

        boolian = true

    }

}

console.log(sequence)

}

2 Likes

Pyramid

for (let asdf = "#";asdf.length <= 7;asdf += "#"){
console.log(asdf)
}

FizzBuzz

for (let i = 1; i <= 100; i++){
 let whatForPrints = "";
 if (i%3 == 0) whatForPrints += "Fizz";
 if (i%5 == 0) whatForPrints += "Buzz";
 if (whatForPrints.length == 0) whatForPrints = i.toString();
 console.log(whatForPrints);
}

Chessboard

let size = parseInt(prompt("¿De qué tamaño?"));
let board = "";
for(let i = 0;i<size;i++){
 for(let j = 0;j<size;j++){
  if((j+i)%2 == 0) {
   board += "#"
  } else {
   board += " "
  }
 }
 board += "\n"
}
console.log(board);
2 Likes

Fizzbuz:
let limit = 100;

let curNum = 1;
let fizTrue = false;
let buzTrue = false;
let fizbuzTrue = false;


for (curNum =1; curNum <101; curNum++) {

    if ((curNum%3)==false){fizTrue=true;}else{fizTrue=false;}
    if ((curNum%5)==false){buzTrue=true;}else{buzTrue=false;}

    document.write("<h2>" + curNum);
    if (fizTrue&&buzTrue){document.write(" : FizzBuzz</h2>");}
    else if (fizTrue){document.write(" : Fizz</h2>");}
    else if (buzTrue){document.write(" : Buzz</h2>");}
    else document.write(" </h2>");

Chessboard

let sqrSize = prompt("What size grid do you want?");

let curRow = 1;
let oddRow = true;



for (curRow = 0; curRow<sqrSize; curRow++) {
    if ((curRow%2)==true){
        //0 row is an even row, therefore we start the writeString with a blank
        document.write("<h2> ");
        for (let colNum =0; colNum<sqrSize; colNum++){
            if (colNum%2){document.write("_");} else {document.write("#");}            
        }
        document.write("</h2>");
    } else {
        //1 row is an even row, therefore we start the writeString with a #
        document.write("<h2> ");
        for (let colNum =0; colNum<sqrSize; colNum++){
            if (colNum%2){document.write("#");} else {document.write("_");}            
        }
        document.write("</h2>");
        

    }

}
2 Likes

var tf = true;
var cntr = 0;
var astrc = “"
while(cntr<10){
document.write("

" + astrc + “

”);
//console.log(astrc);
astrc = astrc + "
”;
cntr+=1;
2 Likes
  1. Looping a 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);
    

    }


  1. FizzBuzz

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


  1. Chessboard

var size = 8;
var board = “”;

for (let outer = 0; outer < size; outer++) {    
	for (let inner = 0; inner < size; inner++) {  
    if ((outer + inner) % 2 == 0)
		    board += " ";
        else
        board += "#";
  }
        board += "\n";
}
console.log(board);

Without the help of internet, I would never have solved Q3. I still don’t get the alternating process. I have tried for several days for these questions.

This is so incredibly hard for me, have already given up so many times up to this point. I have only reached up to this level of the Javascript Programming course after several months. Everybody else must be insanely smart.

1 Like

1. Triangle

let hash= " ";
let counter= 0;
while (counter<8) {
console.log(hash); 
hash+="#";
counter++;
}

2. FizzBuzz

for (i=0; i<=100; i++)
{let word =""; 
if (i%3==0) {word="fizz"; console.log(word); } 
if (i%5==0) {word="buzz"; console.log(word); } 
if (i%3==0 && i%5==0) {word="fizzbuzz"; console.log(word); }
else {console.log(i);}
}

2. Chessboard
It’s a shame, but unfortunately it took me too long to fugure out how to apply newline character and create an 8x8 grid… Managed to make simplified edition of the program:

let string = " # # # #"; 
let string2="# # # # "; 
for (i=1; i<=8; i++) 
{if (i%2==0) 
{console.log(string2);}
else {console.log(string);}
}
1 Like

Thanks a lot mate , i already had my reseach and finded out , but thank you once again ! :wink:

Below .js file

var size1 = "8";
var size2 = "18";
var board = "";

// y = rows
// x = columns

for (var y = 0; y < size1; y++){
  for (var x = 0; x < size2; x++){
    if ((x + y) % 2 == 0){
      board += " ";
    }
    else {
      board += "#";
    }
  }
  board += "\n";
}

console.log(board);
document.write(board);

Below my index page

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Chessboard Practise</title>
  </head>
  <body>
    <h1>Chessboard creator</h1>
    <p><strong>Instantly create a Chessboard with this tool.</strong></p>
    <hr>
    <p>Define how many rows and how many columns you would like below</p><br><br>

    <form acion="Chessboard.js" method="get">
      <div>
        <label for="size1">Number of rows</label>
        <input type="number" class="size" name="rows" align="centre" id="size1" min="2" placeholder="enter number" required>
      </div>
      <br>
      <div>
        <label for="size2">Number of columns</label>
        <input type="number" name="columns" align="centre" id="size2" min="2" placeholder="enter number" required>
      </div>
      <br>
      <button type="reset" value="clear" align="centre" name="reset">Reset</button>
      <button type="submit" value="submit" id="Go" name="Go"> Go </button>

    </form><<pre><script src="Chessboard.js"> </pre></script>



  </body>

</html>

question: How can i get the user input from index page into my variable? If some one can explain to me how that works, or point me to an article or video where it is explained, that would be great! thanks!

1 Like

Hi

for Practice exercise - Boolean’s & If,Else, it seems that “else” function not working out for some reason. it works when I use if only. It does this for all the questions. This below is part of exercise 20 Help please! :slight_smile:

Q20 e statements***, exercises 17 to 20, it seems to work if I only use “if” but not when I use else

1 Like

Hey!
How many people here almost crashed their computer
while playing around with while loops ?
:sweat_smile: :sweat_smile: :sweat_smile:

I’ve had my lesson, I know now to ALWAYS
make my code become false at one point in the loop…

1 Like

In the first exercise, to make a hashtag triangle, I played around with the code Ivan gave us in the video, and somehow, it didn’t work in the console. (I’m also having some problems I think with my Atom program).

But anyways, I tinkered with the code, and I eventually arrived to the same results with this version:

var square= " “;
for(counter=0; counter<7; counter++) {
console.log(square+=”#");}
for(column = 0; column<counter; column++);

If it turns out correctly in the console, it means it’s all good, right?

1 Like
  1. for (x=1;x<=100;x++) {
    if ((x%3==0) && (x%5==0)) {
    console.log (“FizzBuzz”);
    }
    else if (x%3==0) {
    console.log(“Fizz”);
    }
    else if (x%5==0) {
    console.log(“Buzz”);
    }
    else {
    console.log(x +"\n");
    }}
  2. let stringn = “”;
    let stringn1 = “”;
    let inputn = prompt(“Grid Nr please”);
    let gridn = parseInt(inputn);
    for (x=1; x<=gridn; x++) {
    if (x%2!=0) {
    stringn = stringn +" “;
    stringn1 = stringn1 +”#";
    }
    else {
    stringn = stringn + “#”;
    stringn1 = stringn1 +" ";
    }
    }
    for (y=1;y<=gridn;y++) {
    if(y%2!=0){
    console.log(stringn);
    }
    else {
    console.log(stringn1);
    }}
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);
}

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

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

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

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

else {console.log (i); }

}

2 Likes

hey guys, I just want to reach out and ask why my solution is returning undefined. It isn’t showing any errors in Jshint (https://jshint.com/).

my code:
var string = " ";
var slength = string.lenght;
var size = 0;
var targetSize =prompt(“what size grid would you like?”, “enter size”);
//

while (size<= targetSize) {

if ((slength%2)==0) {

var string = (string + " ");
}
else if ((slength%2)!=0){
var string = (string + “#”);
}
else if (slength == targetSize){
console.log(string + “/n”);

var string = (" ");

}

var size = (size+1);
}

any insight would be appreciated.

1 Like
  1. Looping a Triangle (Given solution) - There is a same exercise in previous lessons but using “*” and typing code on the JS console.
    var row_num = 7;
    for (var row = 0; row < row_num; row++){
    var hashtag = “#”;

     for (var column = 0; column < row; column+=1){
       hashtag += "#";
     }
     console.log(hashtag);
    

    }

  2. FizzBuzz - my mistake from the beginning was putting console.log to print “Fizz” or “Buzz” or “FizzBuzz”, this works but you still print the number that is divisible by 3 or 5 or both.
    for (let number = 1; number <= 100; number++){
    let printThis = “”;
    if(number % 3 == 0){
    printThis += “Fizz”;
    }
    if(number % 5 == 0){
    printThis += “Buzz”;
    }
    console.log(printThis || number);
    }

  3. Chessboard - Still trying my best HAHAHA, I’m a beginner also

1 Like