- It is a good practise to do your self. keep it up.
These exercises are taking me hours…no experience prior to November 9!. Same boat, I get the logic but the syntax and remembering what works/doesn’t is challenging. Check out this link I found. There’s a bunch of examples, fill in the blank type thing. It’s helped me a bit haha.
My fizzbuzz code was 10x the size and took me several hours haha. Just started learning Javascript this week. Your solutions make it look so easy!
FizzBuzz
for(var a = 1 ; a <=100 ; a++){
var output = “” ;
if(a % 3 == 0 && !(a % 5 == 0))output += “fizz” ;
if(a % 5 == 0 && !(a % 3 == 0))output += “buzz” ;
if(a % 3 == 0 && a % 5 ==0)output += “fizzbuzz” ;
console.log(output || a)
}
Triangle
var stars = “” ;
for(counter = 0 ; counter < 10 ; counter ++){
console.log(stars = stars + “*”);
}
Chessboard
var stars = “” ;
for(counter = 0 ; counter < 4 ; counter ++){
console.log("# # # # # # # # “) ;
console.log(” # # # # # # # #") ;
}
well i have to admit .it was no easy maybe because this exercises are my very first ones, no previous knowledge in coding i really hope to undersant better about it i think with more practice .
// FIZZBUZZ
for(let n = 1; n <= 100; n++){
let output = “”;
if(n % 3 == 0) output += “Fizz”;
if(n % 5 == 0 && n % 3 != 0) output += “Buzz”;
console.log(output || n);
}
// CHESSBOARD
let size = 8;
let board = "";
for (y=0; y<size; y++){
for (x=0; x<size; x++){
if ((x+y) % 2 == 0){
board += " ";
}
else {
board += "#";
}
}
board += "\n";
}
console.log(board)
FizzBuzz:
for (var num = 1; num <= 100; num++) {var result = “”;
if (num % 3 == 0) result += “Fizz”;
if (num % 5 == 0) result += “Buzz”;
console.log(result || num);}
Chessboard:
for (var fullchess = 0 ; fullchess < 4 ; fullchess++){
var line1 = " # # # #";
var line2 = "# # # # ";
console.log(line1)
console.log(line2)
}
I too was confused by this as I didn’t realise that the 14 I was seeing at the bottom was not the console logging as part of the loop but merely reflecting the value of number
. I take it the arrow pointing left is the indicator for this being the case
============================
Looping a Triangle
var printThat = “#”;
for(var row = 0; row < 7; row++){
console.log(printThat)
printThat += “#”;
}
============================
FizzBuzz
x = “Fizz”
y = “Buzz”
for(var num=1; num<101; num++) {
if (num % 3 === 0) {
console.log(x)
}
if (num % 5 === 0 && num % 3 > 0){
console.log(y)
}
if (num % 5 === 0 && num % 3 === 0){
console.log(x+y)
}
}
============================
Chessboard:
var x = “# # # # "
var y = " # # # #”
for(a=0;a<8;a++){
if (a===0||a===2||a===4||a===6){
console.log(x)
}
else if (a===1||a===3||a===5||a===7) {
console.log(y)
}
}
//Question 1 - Looping a triangle version 1 (long)
<script type="text/javascript">
let hashes = "";
for(var i = 0; i < 7; i++){
hashes += "#";
console.log(hashes);
}
</script>
//Question 1 - Looping a triangle version 2 (short)
<script type="text/javascript">
for(var i = "#"; i.length <= 7; i += "#"){
console.log(i);
}
</script>
//Question 2 - FizzBuzz
<script type="text/javascript">
for (var i = 1; i < 101; 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);
}
}
</script>
//Question 3 - Chessboard version 1 (long)
<script type="text/javascript">
let a = parseInt(prompt("Please select the size of your Chessboard"));
let row1 = "";
let row2 = "";
let string = "";
for(var i = 1; i <= a ; i++){
if (i % 2 == 0){
row1 += " ";
row2 += "#";
} else {
row1 += "#";
row2 += " ";
}
}
for (var i = 1; i <= a; i++) {
if (i % 2 == 0) {
string += row1 + '\n';
} else {
string += row2 + '\n';
}
}
console.log(string);
</script>
//Question 3 - Chessboard version 2 (short)
<script type="text/javascript">
let size = parseInt(prompt("Please select the size of your Chessboard"));
let boardString = "";
for (x = 0; x<size; x++){
for (y = 0; y<size; y++){
if( (x+y) % 2 == 0){
boardString += " ";
} else {
boardString += "#";
}
}
boardString += "\n";
}
console.log(boardString);
</script>
Quite impressed here. I originally had if (number = %3){ but that was my only mistake.
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);
}
Triangle Loop:
var text = “#”;
var row = 0;
while(row < 7){
console.log(text);
text+= “#”;
row++;
}
FizzBuzz Loop:
var n = 1;
for ( let row = 0; row < 100; row++){
if(n % 3 == 0){
console.log(“Fizz”);}
if(n % 5 == 0){
console.log(“Buzz”);}
console.log(n);
n++;
}
Chessboard Loop:
var txt = “”;
var dim = 8;
for (let row = 0; row < dim; row++){
for (let col = 0; col < 8; col++){
if((row+col) % 2 == 0){txt += “:”;}
else {txt+= "P ";}
}txt += “\n”;
}console.log(txt);
//Triangle
var n = 7;
count = 0;
string = "";
while (count<n) {
string = string + "#";
console.log (string);
count++;
}
//FIZZBUZZ
var n = 100;
for (count=1; count<=n; count++){
if (((count % 3) == 0) && ((count % 5) == 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 size = 8;
var string = "";
var char = " ";
for (row=1; row<=size;row++){
if (row%2==0){
char = "";
}
else {
char = "#";
}
for (col=1;col<=size;col++){
if (char == '#'){
char = ' ';
}
else {
char = '#';
}
string = string + char;
}
string = string + '\n';
}
console.log (string);
Hi, Quick question. I tried to write the star code shorter, and it doesn’t start from the first star. Why is that, I don’t understand the difference. They both have the sameprint+="*".
this is
The normal code:
var x=10
for(y=0;y<x;y++){
var print="";
for(z=0;z<y;z++){
print+=""}
console.log(print);}
and this is shortened:
var print="";
for(y=0;y<10;y++){
print+="";
console.log(print);}
There are stars in between the " " but it doesn’t show here??
I think I understood:D:D:D:D…It is because the second for is not smaller < than the first for so it can’t be printed.
now this was fun:)))))
FizzBuzz
for (i = 1; i <= 100; i++){
if ((i % 3 === 0) && (i % 5 === 0)){
console.log(“FizzBuzz35”);
}
else if ((i % 3 === 0) && (i % 5 !=0)){
console.log(“Fizz3”);
}
else if ((i % 3 != 0) && (i % 5 === 0)) {
console.log(“Buzz5”);
}
else {
console.log(i);
}
}
Chessboard
var n = parseInt(prompt(“Enter size of the Chessboard”));
for (var i = 0; i < n; i++) {
if (i % 2 === 0){
var textC = “”;
for (j = 0; j < n/2; j++){
var textC = textC + “0”;
if ((n % 2 != 0) && (j >= n/2-1)){break}
var textC = textC + “#”;
}
console.log(textC + “\n”);
}
if (i % 2 != 0){
var textC ="";
for(j = 0; j < n/2; j++){
var textC = textC + “#”;
if ((n % 2 != 0) && (j >= n/2 -1)){break}
var textC = textC + “0”;
}
console.log(textC + “\n”)
}
}
Chessboard - Enter number of rows and columns
var y = parseInt(prompt(“Enter the number of rows”));
var x = parseInt(prompt(“Enter the number of columns”));
var textC = “”;
for (i = 1; i <= x; i++){
for (j = 1; j <= y; j++){
if ((i+j) % 2 != 0){
var textC = textC + “#”;
}
else {
var textC = textC + “0”;
}
} console.log(textC + “\n”); textC = “”;
}
- Triangle
var a=1;
var star=" “;
while(a<8){star=star+”#"; console.log(star); a++}
2 )Fizz Buzz
for(var a=1;a<=100;a++)
{if(a%3==0&&a%5==0){console.log(“fizzbuzz”);}
else if(a%3==0){console.log(“buzz”);}
else if(a%5==0){console.log(“fizz”);}
else{console.log(a);}}
- Chessboard
var a=" # #"
var b="# # "
var c=1;
for(c=1;c<9;c++){if(c%2==0){console.log(a+a);}
else {console.log(b+b);}}
The solution I found on google for triangle was a bit more concise than the solution found in the book I hope thats ok.
Triangle:
for(var i=1; i<=7; i++){
console.log("*".repeat(i));
}
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);
}
Checkerboard:
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);
Triangle:
for (let line = “#”; line.length < 8; line += “#”)
console.log(line);
Fizz Buzz:
for(a=1;a<100;a++){
if(a%3==0)console.log(“fizz”);
if(a%5==0)console.log(“buzz”);
else{console.log(a);}}
chessboard
let board = “”;
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 8; x++) {
if ((x + y) % 2 == 0) {
board += " ";
} else {
board += “#”;
}
}
board += “\n”;
}
console.log(board);