Chapter 2 Exercises

nice! trying to grasp what youve done… cheers.

1 Like

Really cool, just makes me feel sheite… lol

1 Like

Triangle

let a = '#';
for (i = 0; i < 7; i++) {
	console.log(a);
	a += '#';
}

FizzBuzz

let b = 1;
while (b <= 100) {
	if (b % 3 == 0 && b % 5 == 0) {
		console.log('FizzBuzz');
	}
	else if (b % 3 == 0) {
		console.log('Fizz');
	}
	else if (b % 5 == 0) {
		console.log('Buzz');
	}
	else {
		console.log(b);
	}

	b++;
}

Chessboard

let str = '',
	size = 8;

for (let y = 0; y < size; y++) {
	for (let x = 0; x < size; x++) {
		if ((y + x) % 2 == 0) {
			str += ' ';
		}
		else {
			str += '#';
		}
	}
	str += '\n';
}
console.log(str);
1 Like

Have I missed something? I feel like we jumped from doing things a beginner can start to grasp, to code that we haven’t really covered yet. Unless, like I say, I have missed something…

1 Like
var size = 8;
var board = "";
		for (var b = 0; b < size; b++){
			for (var a = 0; a < size; a++){
				if ((a+b) % 2 == 0) 
					board += " ";
				else
					board += "#";			
		}	board += "\n";
	}
		console.log(board);

ps: I can smell my neurons burning out… lol, is that normal while coding?!

Edit @ivga80: You can use the preformattet text to show your code. :wink:

1 Like

Triangle
for (var result = “#”; result.length <=7; result = result + “#”)
console.log(result);

Fizzbuzz
for(number=0;++number<101;console.log(number%5?x||number:x+‘Buzz’))x=number%3?’’:‘Fizz’

Chessboard

var chessBoard= '',
    size= 8,
    c;

for(var i = 0 ; i < size ; i++) {
  c= i%2 ? '# ' : ' #';
  for(var j = 0 ; j < size/2 ; j++) {
    chessBoard+= c;
  }
  chessBoard+= '\n';
}

console.log(chessBoard);

Disclaimer, I do not understand this but I have been trying to understand it for 2 days. It is hard to Google help for this without coming across the answer. IMO this exercise is too hard for a beginner, seems we just skipped maybe 3 or 4 more tutorials and jumped to this. Again, just my opinion. Up until this it seemed it was progressing at a sensible pace.

1 Like

This is the second exercise in the book and it can be tricky for some in the beginning. But this exercise shows us that programming is not easy, it’s a lot of thinking and trying and failing. But that’s how we learn in the end. :wink:

I think 2 days was fast. I think most people use a lot more than that. I know I did. :man_teacher:
Be patient and learn at your own pace.
Ivo

1 Like

Hey @Mark1, if it makes you feel better… the chessboard exercise toke me two days also… my brain even started to smell… best, m8

1 Like

ha! Yeah i think I just need to do more reading and watching ion videos. Just felt slightly lost as I don’t remember covering it in the course up until now, apart from a little in the book.

Exercise 1: LOOPING A TRIANGLE
for (let hashtag = “#”; hashtag.length <= 7; hashtag = hashtag + “#”) {
console.log(hashtag);
}

Exercise 2: FIZZBUZZ
Part 1:
let number = 1
while (number <= 100) {
if (number % 3 == 0) {
console.log(“Fizz”)
}
else if (number % 5 == 0) {
console.log(“Buzz”)
}
else {
console.log(number)
}
number = number + 1
}
Part 2:
let number = 1
while (number <= 100) {
if (number % 3 == 0 && number % 5 != 0) {
console.log(“Fizz”)
}
else if (number % 5 == 0 && number % 3 != 0) {
console.log(“Buzz”)
}
else if (number % 3 == 0 && number % 5 == 0) {
console.log(“FizzBuzz”)
}
else {
console.log(number)
}
number = number + 1
}

Exercise 3: CHESSBOARD
let count = 2
let character
let grid = " "
while (count < 72) {
if (count % 2 != 0 && count % 9 != 0) {
character = " "
}
else if (count % 2 == 0 && count % 9 != 0) {
character = “#”
}
else {
character = “\n”
}
grid = grid + character
count = count + 1
}
console.log(grid)

1 Like
  • FizzBuzz (a):
 for( var number = 0; number <= 100; number++ )
 {
   if( number % 3 == 0 ) console.log( "Fizz" );
   else if( number % 5 == 0 ) console.log( "Buzz" );
   else console.log( number );
 }
  • FizzBuzz (b):
for( var number = 0; number <= 100; number++ )
{
  var divBy3 = ( number % 3 == 0 );
  var divBy5 = ( number % 5 == 0 );
                
  if( divBy3 && divBy5 ) console.log( "FizzBuzz" );
  else if( divBy3 ) console.log( "Fizz" );
  else if( divBy5 ) console.log( "Buzz" );
  else console.log( number );
}
  • ChessBoard (a):
for( var row = 0; row < 8; row++ )
{
  var element = ( row % 2 == 0 ) ? " " : "#";
  var rowText = "";
  for( var column = 0; column < 8; column++ )
  {
    rowText += element;
    element = ( element == "#" ) ? " " : "#";
  }
  console.log( rowText );
}
  • ChessBoard (b):
var size = 8;
for( var row = 0; row < size; row++ )
{
  var element = ( row % 2 == 0 ) ? " " : "#";
  var rowText = "";
  for( var column = 0; column < size; column++ )
  {
    rowText += element;
    element = ( element == "#" ) ? " " : "#";
  }
  console.log( rowText );
}
2 Likes

The best thing is to practice coding… when you have done the assignment, try to spend 1 day to just do the assignments again… mix them up… What ever… We dont learn by just watching videos. We learn when we code, let the brain get used to thinking and writing code. If you do 2-3 hours learning and 1 hour just coding, everyday for 6 months… That`s when you will really see progression…:star_struck:
You can do it! :hugs:

5 Likes

FizzBuzz

for (var i = 1;i <= 100 ;i++ ) { if ((i % 5 == 0) && (i % 3 == 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); } }

Chessboard

let square = “”;
let size = 8;
for (let height = 1; height <=size; height++){
for (let width = 1; width <=size; width++){
if(( !(height % 2 == 0) && !(width % 2 == 0)) || ( (height % 2 == 0) && (width % 2 == 0) )) square += " ";
if( (!(height % 2 == 0) && (width % 2 == 0)) || ((height % 2 == 0) && !(width % 2 == 0))) square += “#”;
}
console.log(square);
square = “”;
}

Note:
I have noticed that in the examples from the book the Author solved the problems with a bit different code. If I have achieved the same result but with different code, what is the biggest difference besides the size of the code? Does the size have an impact on the time of the execution?

1 Like
<title> great website </title>
<script>

//WRITE EXERCISE CODE
// LOOPING A TRIANGLE
var numRows = 7;

for (var row = 0; row < numRows; row++){
  var toPrint = "#";

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

//FIZZBUZZ
var x = 100;
for(var i = 0; i <= x; i++){
    if(i % 3 == 0){
      console.log("Fizz");
    }
    else if(i % 5 == 0){
      console.log("Buzz");
    }
    else if(i % 3 == 0 && i % 5 == 0){
      console.log("FizzBuzz");
    }
}

//CHESSBOARD
for (var row = 0; row < 8; row++){
  var toPrint = " ";

  for(var column = 0; column < 8; column++){
    if((row % 2 == 0) || (column % 2 == 0)){
      toPrint += " ";
    }
    else(toPrint += "#");

    if((row % 2 == 0) && (column % 2 == 0)){
      toPrint += "#";
      }
      else(toPrint += " ");
    }

  console.log(toPrint);
}


</script>
1 Like
  1. TRIANGLE
    for (let row = 0; row < 7; row++) {
    let toPrint ="#";

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

  1. FIZZBUZZ
    for(y = 1; y < 101; y++){
    var multipleOfThree = y % 3 === 0;
    var multipleOfFive = y % 5 === 0;
    if(multipleOfThree && multipleOfFive){
    console.log(“FizzBuzz”);
    }
    else if(multipleOfThree){
    console.log(“Fizz”);
    }
    else if(multipleOfFive){
    console.log(“Buzz”);
    }
    else {
    console.log(y);
    }
    }

  2. CHESSBOARD
    var board = “”;
    for(y = 0; y < 8; y++) {
    for(x = 0; x < 8; x++) {
    if((x + y) % 2 ==0) {
    board += “ “;
    } else {
    board += “#”;
    }
    }
    board += “\n”;
    }
    console.log(board);

1 Like
Exercises done by myself
<body>

Time spent on coding everything by myself: 6h

Feeling at the end:

  <script>
  //Looping a triangle
  for (a=0; a<7; a++){
  var print = "#";
  for (b=0; b<a; b++){
  print += "#"
  }
  console.log(print)
  }

  //FizzBuzz
  for (a=0; a<101; a++){
  if(a%3==0){console.log("Fizz")}
  if(a%5==0){console.log("Buzz")}
  if((a%3==0) && (a%5==0)){console.log("FizzBuzz")}
  console.log(a)
  }

  //ChessBoard
  for(row=0;row<8;row++)
  {
    var print="";
    for(column=0;column<8;column++)
    {
      if((row+column)%2==0){print+=" ";}else{print+="#";}
    }
    console.log(print);
  }
  </script>
2 Likes

nice job! I like it. :+1:
Ivo

1 Like

thank you very much for the encouragement :pray:

1 Like

1 Like

1. Triangle

for (let tag = “#”; tag.length < 8; tag += “#”) {
console.log(tag)
}

2. FizzBuzz

for (let number = 1; number < 101 ; number += 1) {
if (number % 3 == 0) {
console.log(“Fizz”);
}
else if ((number % 5 == 0) && (number % 3 != 0)) {
console.log(“Buzz”);
}
else console.log(number)
}

for (let number = 1; number < 101 ; number += 1) {
if ((number % 3 == 0) && (number % 5 != 0)) {
console.log(“Fizz”);
}
else if ((number % 5 == 0) && (number % 3 != 0)) {
console.log(“Buzz”);
}
else if ((number % 5 == 0) && (number % 3 == 0)) {
console.log(“FizzBuzz”);
}
else console.log(number)
}

3. Chessboard

let size = 8;
let chess = “”;

for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if ((x + y) % 2 == 0) {
chess += " ";
} else {
chess += “#”;
}
}
chess += “\n”;
}

console.log(board);

1 Like