Triangle
for(var line="#"; line.length<8; line+="#")
console.log(line);
fizzbizz
for(number=0; number<=100;number=number+1){
if(number%3==0){console.log(“fizz”)}
if(number%5==0){console.log(“buzz”)}
if(number%15==0){console.log(“fizzbuzz”)}
else{console.log(“number”)}}
chessboard
A=" ####"
B="####"
for(i=0; i<=8; i+=2){
console.log(B+"\n");
console.log(A+"\n");
}
Exercise 1
for (let line = “#”; line.length < 8; line += “#”)
console.log(line);
(Couldn’t do it on my own. Found this to be the easiest solution.)
After some more learning attempted the other 2 exercises.
Exercise 2
for (numwrite = 1; numwrite <=100; numwrite++) {
if(numwrite % 3 === 0 & numwrite % 5 !== 0) { console.log("Fizz"); continue; } if(numwrite % 5 === 0 & numwrite % 3 !== 0) { console.log("Buzz") continue; } if(numwrite % 3 === 0 & numwrite % 5 === 0) { console.log("FizzBuzz") continue; } console.log(numwrite); if (numwrite > 200) { break; } }
(Did it on my own. Eventually looked at the solution which seems pretty short compared to mine. I also added an additional if statement with a break because I used to get an infinite loop. Helped me save some time.)
Exercise 3
for (height = 1; height <= 8; height++) {
if (height % 2 !== 0) { var space = " "; for (infcounter = 1; infcounter <2;) { if (space.length < 8 & space.length % 2 !== 0) { space = space + "#" } if (space.length <8 & space.length % 2 === 0) { space = space + " " } if (space.length === 8) { console.log(space) break; } } } if(height % 2 === 0) { var hash = "#"; for (infcounter2 = 1; infcounter2 <2;) { if (hash.length < 8 & hash.length % 2 !== 0) { hash = hash + " " } if (hash.length < 8 & hash.length % 2 === 0) { hash = hash + "#" } if (hash.length === 8) { console.log(hash) break; } }
}
}
(Took me over an hour and it for sure looks way more complicated than the actual solution from the book. Disadvantage in my code is that I need to change 7 number in order to make a new dimension of the chessbaord. However I can make it different width and height. Example 10x8 or 8x10, while the original solution must be either 8x8 or 10x10. Could be written much more better either way.)
I know it isn’t much and that I still have a long way to go, but I am pretty stoked about the fact that I struggled through writing this code and it actually works!!
Fizzbuzz assignment:
var max_count = 100;
for(count = 1; count<=max_count; count++){
if((count % 3 == 0) && (count % 5 != 0)){
console.log("fizz")
}
else if((count % 5 == 0) && (count % 3 != 0)){
console.log("buzz")
}
else if((count % 5 == 0) && (count % 3 == 0)){
console.log("fizzbuzz")
}
else {
console.log(count)
}
}
Is anyone able to give me tips on how I can write this more efficiently? I know this is functional but perhaps not the most concise format.
Thanks everyone!
//Doing exercise 1
var fizz = "Fizz";
var buzz = "Buzz";
var calc = 0;
for( var counter = 0; counter < 100; counter++){
if (calc % 3 == 0 && calc % 5 == 0){
console.log("number: " + calc + " " +fizz + buzz);
}
if (calc % 3 == 0 ){
console.log("number: " + calc + " " +fizz);
}
if (calc % 5 == 0) {
console.log("number: " + calc + " " + buzz);
}
calc ++;
}
//Doing exercise 2
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 += " ";
}
else{board += "#";}
}
board += "\n";
}
console.log(board);
Hi,
To be honest I already passed trough Eloquant JS chapters 1-4 last years but its a nice little refresh.
I had the exercise done for node.js/other console tools but not made for print out in an html page.
At the moment I’m creating a website by following the exercise in the course. So I tough it would be interesting to put each exercise in an individual page and link them to my index.html in a
- unordered list. Its not hosted online yet so I’ll just copy paste the script tags here:
Lopped triangle
<script>
/*
Execise create a loop function that print a triangle like this:
#
##
###
####
#####
######
#######
*/
// here is my first version
function loopedTrian() {
let myTriangle = "";
for (var i = 0; i < 7; i++) {
myTriangle += "#";
console.log(myTriangle);
};
};
// based on the book
function lT2() {
for (var tria = "#"; tria.length <= 7; tria += "#") {
console.log(tria);
};
};
//---------- For HTML
let myTriangle = "";
for (var i = 0; i < 7; i++) {
myTriangle += "#";
document.write(myTriangle+ "<br>");
};
</script>
FizzBuzz
<script>
/*
Print a function that return numbers from 1-100 and
"Fizz" for number divisible by 3
"Buzz" for number divisible by 5
"FizzBuzz" for number divisible by 3 and 5
*/
function fizzB1() {
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);
}
}
}
function fromBookV() { //had to repair it: https://eloquentjavascript.net/2nd_edition/code/#2.2
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0){
output += "Fizz";
} else if (n % 5 == 0){
output += "Buzz";
}
console.log(output || n);
}
}
function fizzB2() {
for (var i = 1; i <= 100 ; i++) {
var text = "";
if (i % 3 == 0 && i % 5 == 0) {
text += "FizzBuzz";
} else if (i % 3 == 0) {
text += "Fizz";
} else if (i % 5 == 0) {
text += "Buzz";
}
console.log(text || i);
}
return "Done!";
}
// ------------------ For html
for (var i = 1; i <= 100 ; i++) {
var text = "";
if (i % 3 == 0 && i % 5 == 0) {
text += "FizzBuzz";
} else if (i % 3 == 0) {
text += "Fizz";
} else if (i % 5 == 0) {
text += "Buzz";
};
if (text) {
document.write(text + "<br>");
} else {
document.write(i + "<br>");
};
};
</script>
Chestboard
I want a bit crazy on this one making border and little style addition lol. I feel like theres a couple of pieces that should be better coded because at the moment a lot need to be manually changed if the board size change instead of just adding arguments but well I dont feel like passing to much hours on it at the same time
<script>
function chestBPrint(size) {
var board = "",
head = "",
bottom = "";
//Chestboard frames (top and bottom)
function frames(frameSide, strChar) {
while (frameSide.length <= size*2) {
frameSide += strChar;
}
frameSide += "<br />";
return frameSide;
}
board += frames(head, "_");
//Rows (height)
for (var b = 0; b < size; b++) {
//left border
board += "|";
//Cells (width)
for (var i = 0; i < size; i++) {
if ((b + i) % 2 == 0) {
board += "__";
} else {
board += "##";
}
}
//right border and close row
board += "|<br />";
}
board += frames(bottom, "¯");
return board;
};
document.write("My Chestboard!<br />");
document.write(chestBPrint(8));
</script>
Triangle Loop
for (let line = “#”; line.length < 8; line += “#”)
console.log(line);
Fizz Buzz
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);
}
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”;
}
console.log(board);
Admittedly, the code was ultimately taken from someone else. For both exercises I figured out the solution rather quickly, as far as the logic goes. However, I didn’t know how to put the logic into code. That was my primary hang up. I used google until I didn’t know what or how else to search for solutions. Then I came here and to my surprise I had the right idea for statements, but not the experience to put my ideas into functional code. Here is what I came up with (which is heavily taken from another user).
FizzBuzz:
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);}
My husband thought it would be fun for me to make the checkerboard have two variables, one for the columns and one for the rows, so I modified it, and this was my final result.
Checkerboard:
var sizeOne = 8;
var sizeTwo = 8;
var board = " ";
for(let y = 0; y < sizeOne; y++ ){
for (let x = 0; x < sizeTwo; x++) {
if ((y + x) % 2 == 0) {
board += " ";}
else {board += “#”;}
}
board += “\n”;}
console.log(board);
Good luck to the rest of you out there! Slowly and surely we’re learning to code!
Hash triangle
<script>
let hash = "#";
for (let counter = 0; counter < 7; counter++){
// document.writeln("<p>" + hash + "</p>");
console.log(hash);
hash = hash + "#";
}
</script>
FizzBuzz
<script>
let num = 1;
while (num <= 100){
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);
}
num++;
}
</script>
ChessBoard
`<script>
let size = 10;
let result = "";
for (let height = 0; height < size; height++){
if (height % 2 === 0){
for (let width = 0; width < size; width++ ){
if (width % 2 === 0){
result += " ";
} else {
result += "#";
}
}
} else {
for (let width = 0; width < size; width++ ){
if (width % 2 === 0){
result += "#";
} else {
result += " ";
}
}
}
result += "\n";
}
console.log(result);
</script>`
Looping a Triangle
let toPrint = "";
for(let row = 0; row < 7; row++){
toPrint += "#";
document.write(toPrint + "<br>");
}
FizzBuzz
for(let number = 1; number <= 100; number++) {
let isFizz = (number % 3 == 0);
let isBuzz = (number % 5 == 0);
if(isFizz && isBuzz) { document.write("FizzBuzz <br>"); }
else if(isFizz) { document.write("Fizz <br>"); }
else if(isBuzz) { document.write("Buzz <br>"); }
else { document.write(number + "<br>"); }
}
ChessBoard
var size = 8;
function printChessLine(size, numOfElemInExample, example) {
for (var x = 0; x < size/numOfElemInExample; x++) { document.write(example); }
document.write("<br>");
}
for (let y = 0; y < size; y++) {
printChessLine(8, 2, " #");
printChessLine(8, 2, "# ");
}
Looping 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);
}
Chess Board:
//First attempt:
//console.log(" # # # #\n# # # # \n # # # #\n# # # # \n # # # #\n# # # # \n # # # #\n# # # # \n");
//Second attempt: I needed to check the answer. Could not figure out the if ((x + y) % 2 == 0) step by myself.
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 += " ";
}
else{board += “#”;}
}
board += “\n”;
}
console.log(board);
FizzBuzz
for(var fizzcount = 1; fizzcount <=100; fizzcount++){
if(fizzcount % 5 == 0 && fizzcount % 3 == 0) {
document.write("FizzBuzz
");
}
else if (fizzcount % 5 == 0) {
document.write("Buzz
");
}
else if (fizzcount % 3 == 0) {
document.write("Fizz
");
}
else{
document.write(fizzcount + “
”);
}
}
I intensely had to use the manuals for these, JS makes my head hurt!
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);
}
Fizzbuzz
for(i= 1; i <= 100; i++){
if(i % 3 === 0 && i % 5 === 0){
console.log(“FizzBuzz”);
continue;
}
else if(i % 3 === 0){
console.log(“Fizz”);
continue;
}
else if(i % 5 === 0){
console.log(“Buzz”);
continue;
}
console.log(i);
Chessboardstrong text
let size_board = 8;
let board = “”;
for(let white = 0; white < size_board; white++) {
for(let black = 0; black < size_board; black++) {
if((white + black) % 2 === 0) {
board += " ";
} else {
board += “#”;
}
}
board += “\n”;
}
console.log(board);
FIZZBUZZ:
for(var num = 1; num <= 100; num++){
if((num%3==0) && (num % 5==0)){
console.log(“FizzBuzz” +""+num);
} else if(num % 3 ==0){
console.log(“Fizz”+" “+num);
} else if(num% 5==0){
console.log(“Buzz”+” "+num);
} else {
console.log(num);
}
}
CHESS BOARD
var size = 8;
var board=" “;
for (var a = 1; a <= size; a++){
for (var b=1; b<=size;b++){
if ((a+b) % 2 == 0){
board += " “;
} else {
board += “#”;
}
}
board +=”\n”;
}
console.log(board)
ps. I tried looking for explanations to the exercise, but once googled, you get the answer straight away…
FizzBuzz
var num_rows = 100;
for(var row = 1; row <= num_rows; row++){
if(row % 15 == 0)
console.log(“FizzBuzz”);
else if(row % 5 == 0)
console.log(“fizz”);
else if(row % 3 == 0)
console.log(“buzz”);
else
console.log(row);
}
Chessboard
var size = 4;
for(var row = 1; row <= size; row++){
var oddPrint = " #";
var evenPrint = "# ";
for(var autosize = 1; autosize<size; autosize++){
oddPrint += " #"; evenPrint += "# ";
}
if(row % 2 == 0)
console.log(evenPrint);
else
console.log(oddPrint);
}
Looping TRiangle:
for (let line = “#”; line.length < 8; line += “#”)
console.log(line);
FizzBuzz
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);
}
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”;
}
console.log(board);
These exercises are challenging. I have to go back to Chapter 1 to have an idea how to write these exercises.
I am sure that I am the only one.
-
Increasing hash by one
var b="#"
for (b.length=1; b.length<=7; b=b+"#") {
document.write(b,"
");
} -
fizzbuzz
var a=1
var b=3
var c=5
for (a=1; a<=100; a++) {
if (a%b==0 && a%c!=0) {
document.write(“fizz”,"
");
} else if (a%c==0 && a%b!=0) {
document.write(“buzz”, “
”);
} else if (a%b==0 && a%c==0) {
document.write(“fizzbuzz”, “
”);
} else {
document.write(a,"
");
}
} -
Chequerboard
var row = 0;
var cols = 0;
while(row <= 7){
if(row%2 == 0){
document.write(" ");
}
while(cols <= 7) {
document.write( “# “);
cols++;
}
document.write(”
”);
cols = 1;
row++;
}
I Haven’t yet ironed out issues with making the chequerboard a variable number of rows and columns but I am working on it.
Same. Feels really hard for what we’ve learned so far. I was not able to do any of them.
Chapter 2 was not that hard for me but Chapter 3 is quite hard. Kinda got stuck and left the course for some time. Gotta get back to it ASAP.
FIZZBUZZ:
var number = 101;
for(var count = 1; count < number; count++){
if (count % 15 == 0){
console.log("Fizzbuzz");
}
else if (count % 3 == 0){
console.log("Fizz");
}
else if (count % 5 == 0){
console.log("Buzz");
}
else {
console.log(count);
}
}
CHESSBOARD:
var num = 8;
for (var row = 1; row <=num; row++){
var toPrint = "";
for (var col = 1; col <=num; col++){
if (row % 2 == 0){
if (col % 2 == 0) {
toPrint += " ";
}
else(toPrint+="#");
}
else if (col % 2 == 0){
toPrint += "#";
}
else (toPrint+=" ")
}
console.log(toPrint);
}
What am I doing wrong? ChessBoard exercise.
var textToDisplay = “#”;
for(var row = 0; row<8; row=row +1) {
document.write("
# " + textToDisplay + " " + textToDisplay + " " + textToDisplay + “
”);}