HI @Crista could you post your code as code format? please take a look at the picture below.
OK Sorry about that.
FizzBuzz
for(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);
}
}
Checkerboard
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;
}```
thank you. i will change it
//PIRAMID
for (var piramid = â#â; piramid.length<7;piramid+="#") {
console.log(piramid);
}
//FIZZBUZZ
for (var i = 0; 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 && i%5!=0) {
console.log(âFizzâ);
}
else {
console.log(i);
}
}
//CHESSBOARD
var row = 8
var col = 8
for (var rownr = 1; rownr < row+1; rownr++) {
var chessboard = ââ;
for (var colnr = 1; colnr < col+1; colnr++) {
if ((rownr+colnr)%2==0) {
chessboard += â#â
}
else if ((rownr+colnr)%2!=0) {
chessboard += " "
}
}
console.log(chessboard);
}
Triangle:
in while loop:
<script>
var row = 0;
var hash = "#"
while (row < 7) {
console.log(hash);
row++;
hash = hash + "#"
}
</script>
OR in a for loop:
<script>
for (var row = "#"; row.length < 8; row += "#") {
console.log(row);
}
</script>
FizzBuzz Solution:
I printed +row so you can checkâŚ
<script>
for (var row = 1; row <= 100; row++) {
if (row % 3 == 0 && row % 5 == 0) {
console.log("FizzBuzz" + row);
} else if (row % 3 == 0) {
console.log("Fizz" + row);
} else if (row % 5 == 0) {
console.log("Buzz" + row);
}
}
</script>
<script>
var size = 8;
var row = 1;
var column = 1;
var hash = "# ";
while (row <= size && column <= size) {
document.write(hash.repeat(size));
document.write("<br>");
row++;
column++;
}
</script>
Hello,
I do not understand how to do either the FizzBuzz exercise or the Chessboard exercise. Can you please help me out here? How am I supposed to set them up?
There is no way I will be able to figure this out all on my own. I barely understood a majority of the material on loops.
I have always hated word problems in math when I was in school. This is even worse.
Thank you!
Solution on Chapter 2 exercises:
Looping a triangle:
for (let triangle="#"; triangle.length<8; triangle=triangle+"#"){
document.write (triangle)}
FizzBuzz
let printing1=âFizzâ;
let printing2=âBuzzâ;
let printing3=âFizzBuzzâ;
for (let count=1; count<100; count++){
if (count%3===0)console.log(printing1);
if (count%5===0)console.log(printing2);
if (count%3===0 && count%5===0)console.log(printing3);
else console.log (count);
}
ChessBoard
let board="";
for (let i=0; i<8; i++){
for (let c=0; c<8; c++)
if ((i+c)%2===0) board=board+" â;
else board=board+â#";
board=board+"\n";}
Hi @jgentry sometimes things seems difficult than they are.
Lets see the first one,
Write a program that uses console.log to print all the numbers from 1 to 100,
with two exceptions. For numbers divisible by 3, print âFizzâ instead of the
number, and for numbers divisible by 5 (and not 3), print âBuzzâ instead.
When you have that working, modify your program to print âFizzBuzzâ for
numbers that are divisible by both 3 and 5 (and still print âFizzâ or âBuzzâ
for numbers divisible by only one of those).
(This is actually an interview question that has been claimed to weed out
a significant percentage of programmer candidates. So if you solved it, your
labor market value just went up.)
Given
1, Write a program that uses console.log to print all the numbers from 1 to 100(in this sentence, we know that we are gonna need a loop in out function) for(){} and a variable that represent a current number.
2, For numbers divisible by 3, print âFizzâ instead of the
number,(here we have to check if the number divisible by 3, so we need to think of mathematical operation that can be used, a condition and a function for printing the âfizzâ ) things you need to know:- % , condition(if , else if or else) and console.log().
3, for numbers divisible by 5 (and not 3), print âBuzzâ instead.(same as number 2 just change 3 by 5 )
4, When you have that working, modify your program to print âFizzBuzzâ for
numbers that are divisible by both 3 and 5 (you have check to both conditions in one condition, so you have to think of a logical operation like AND or OR) things you need to know:- %, condition(if, else if or else), console.log() and logical operation.
5, (and still print âFizzâ or âBuzzâ
for numbers divisible by only one of those). this is the tricky part, here you need to think of which condition you should check first. is it 2, 3 or 4?
I hope this makes a little bit sense let me know if it helps then we do the same for the third question.
Happy coding,
Abel
Hi @Alexandr_Els could you check post your answer as code format?please take a look at picture below.
I did think that a for loop would be necessary; however, I am having trouble figuring out what to put in the parentheses or the curly brackets. Also, the whole ordeal with the if, if-else, etc. is still difficult for me to understand. Some of this very complex material just seems to go right over my head.
I kept referring to the Eloquent Javascript book, but it seemed to help only a little.
Thank you,
Joanna Gentry
I still barely understand.
-
What does that current number need to be?
-
What needs to go in the parentheses following the
console.log
?
I am very frustrated right now. Please help me. I cannot move forward until this is done.
Thank you!
Hi @jgentry No worries, I understand your frustration. I went through same process first time I tried to learn programming. it is a very common experience.
1, current number is the current number in the loop for(var i = 1; 1 <= 100; i++){
console.log(i);
}
in first loop current number is 1 then second loop will be 2 because it increments by 1 so it goes in that order till 100.
check this link it will help a lot about loop https://www.w3schools.com/js/js_loop_for.asp,
2, console.log() is a built in function. it prints whatever you pass in parentheses. console.log(âFizzâ) or console.log(âBizzâ).
Okay. Maybe this simpler strategy will help.
I keep looking at other studentsâ answers for advice, but that is a temptation that needs to be avoided. Cheating is totally unacceptable. I need to figure this stuff out on my own because I might have to do it like this one day.
Thank you.
I also realized it would be better to perform the exercises in the console of my browser rather than in Atom.
Once I feel that I have the FizzBuzz exercise down, Iâll try the same strategy for the Chessboard exercise. But if I need extra help, Iâll get it.
Again, thanks a lot!
I got your previous reply with the article, but I havenât looked at it yet. Thank you!
Donât you have to break the function at certain points?
no, you do not have to break it
The information in my console looks something like this:
for(var i=1; 1<=100; i++){
}
What needs to go in these curly braces? Iâm trying to get as much help as possible without copying other studentsâ answers or cheating.
Thanks!
you need to try to implement the three condition in the curly braces.