FIZZBUZZ
for(a=1;a<=100;a++){
if(a%3==0||a%5!==0){
document.write(“Fizz”);
}
else if (a%5==0||a%3!==0){
document.write(“Buzz”);
}
else if(a%5==0 && a%3==0){
document.write(“FIZZBUZZ”);
}
else document.write(“a”);
}
FIZZBUZZ
for(a=1;a<=100;a++){
if(a%3==0||a%5!==0){
document.write(“Fizz”);
}
else if (a%5==0||a%3!==0){
document.write(“Buzz”);
}
else if(a%5==0 && a%3==0){
document.write(“FIZZBUZZ”);
}
else document.write(“a”);
}
loop triangle
var row,row_num;
for(row=0;row<=6;row++){
for(row_num=0;row_num<=row;row_num++){
document.write("#");
}
document.write("
");
}
Thanks for the good vibes Hamze_Dirir !!
maybe I can help
<script>
function drawChessBoard(){
let chessBoardString = "";
for(let rows = 0; rows<8; rows++){
for(let columns = 0; columns <8; columns++){
if(rows%2 == columns%2) chessBoardString+="#";
else chessBoardString+=" ";
}
chessBoardString+="\n";
}
console.log(chessBoardString);
}
drawChessBoard();
</script>
Hi All,
Im excited that i figured out fizzbuzz without having to search on google or look at the solution
Can someone tell me if there is anything incorrect with how I did this?
// Fizzbuzz
for(var number = 0; number <= 100; number++){
if ((number % 3 == 0) && (number % 5 == 0)) {
console.log("FizzBuzz");
}
else if (number % 3 == 0) {
console.log("Fizz");
}
else if (number % 5 == 0) {
console.log("Buzz");
}
else (
console.log(number)
)
}
Thanks in advance, appreciate any feedback. Still a total noob to programming.
Triangle
for (var counter = "#"; counter.length <= 7; counter += "#"){
console.log(counter);
}
FizzBuzz
for (var counter = 1; counter <= 100; counter += 1){
if ((counter % 3 == 0) && (counter % 5 == 0)){
console.log("FizzBuzz");
}
else if (counter % 3 == 0){
console.log("Fizz");
}
else if (counter % 5 == 0){
console.log("Buzz");
}
else {
console.log(counter);
}
}
Chessboard
var size = 8;
for (var row = 1; row <= size; row++){
var square = "";
if (row % 2 == 0){
for (var n = 1; n <= size; n++){
if (n % 2 == 0){
square += " ";
}
else {
square += "#";
}
}
console.log (square);
}
else {
for (var n = 0; n < size; n++){
if (n % 2 == 0){
square += " ";
}
else {
square += "#";
}
}
console.log (square);
}
}
Tried chessboard immeditely after and couldnt figure it out haha learning coding is a good ego check
Trying it again with fresh eyes today and hopefully will be able to figure it out.
Hi @Joe90,
Apologies for taking so long in providing you with some feedback on your solutions to these exercises.
From the code you’ve posted, and your comments, you appear to be making excellent progress, and applying the right approach by coming up with your own solutions first, and then looking at the hints to see if there are ways to improve your code even further
Your solution to Looping a Triangle is very innovative and demonstrates a great knowledge of different types of loops. I’m sure you have already discovered how the model answer solves the problem much more concisely, and worked out how that was achieved.
Is your posted solution for FizzBuzz what you came up with before looking at the hints and model answer? If it is, then really well done If it’s an improved version after looking at the model answer, then that’s fine too, as long as you understand the code you’re posting; however, if your original code works fine, I’d encourage you to post that instead, and you could maybe add some comments about how you could improve it based on what you’ve learnt by analysing the model answer or other students’ solutions.
Just a couple of comments about your FizzBuzz:
for
loop condition to fix this.if
statement in your FizzBuzz would usually be written as follows:if (i % 3 == 0) {
printValue = "Fizz";
}
// or
if (i % 3 == 0) printValue = "Fizz";
// curly braces can be omitted as there's only one statement in the code block
In your Chessboard, because you are building up just one long string with line breaks, notice how you can omit the variable square
altogether, and just use pattern
, as follows:
if ((height + width) % 2 == 0) pattern += " ";
else pattern += "#";
Keep on learning
Hey @Bunbo_Hue!
So sorry for taking so long to reply to your cry for help with these exercises
Let’s see if we can get you unstuck…
Firstly, have a read of this post. Hopefully this will show you that you’re not the only one finding these exercises really hard, because for a complete beginner they are a real challenge. The post also includes a summary of my top tips for approaching the course book exercises if you’re finding yourself struggling to do them on your own.
I totally understand how frustrating it can be. If you browse through this topic you’ll see a lot of other students in the same boat. Hopefully that will make you realise that what you’re feeling is completely normal for a complete beginner. You’ll also see that it’s not uncommon for students to spend a long long time solving these three exercises — two days is not unheard of — and so the fact that you’ve solved the first one in just several hours, honestly, that’s good going!
Anyway, let’s look at the exercises:
Looping a Triangle
Your solution is excellent
It’s very different to the model answer, but that doesn’t matter at all. The main goal is to try to solve these with your own method, and, as I’ve mentioned in the post I’ve linked to above, you can spend time comparing your code with the model answer and other students’ solutions afterwards in the review and reflection stage.
I really must stress that, even though it’s just a few lines of code, it’s perfectly normal for it to take a complete beginner several hours to come up with what you’ve coded. This is actually good, because during those hours you are really wrestling with the concepts and you’ll be learning more than you perhaps realise. Without this struggle, in other words if the exercises were easily solved too quickly, you wouldn’t progress as quickly.
Your solution is very unique, and so i can see that it’s all your own work. Feel proud of these 6 lines… they are great start!
FizzBuzz
I think you’ve actually already achieved more than you think. You just need some pointers to help you add the finishing touches. Have a good read and think about this post. The student I’m replying to had the same issues with her code as you have with yours, so I think you will find it really helpful. In fact there is one more issue addressed in this post (concerning the condition for the if
statement that logs “FizzBuzz”) that you don’t have, so you can ignore the following section near the end:
In terms of the exercise Chessboard , read through the tips (in the first post I’ve linked to) until you find the first one you haven’t tried yet, and then work from there. Even if you end up having to look at other students’ solutions that work, or the model answer in the course book, you can still learn a lot from spending time analysing and working through it as I’ve suggested. Even then, work on this exercise doesn’t have to stop there. You can come back and recode it using the method that I outline in Tip 6.
Of course, if you still have questions or need any more help after having tried everything I’ve outlined above and in the linked posts, then just let us know.
Good luck!
Hi @Pacheco,
Sorry for the delay in responding to your post.
Unfortunately, you haven’t actually posted any code!
That’s all we’re seeing!
That sounds very positive if you’re a complete beginner, because these exercises are a real challenge!
Let’s have a look at your actual code first, and then we can help you to get it logging the output to the console.
Hi @linlin00,
This is a really good attempt at FizzBuzz — you’re very nearly there! Let’s sort out the issues, and add the finishing touches…
<br>
element to each string written to the document, and by using string concatenation to add it to the variable name a
(which references each number on every loop): document.write("Fizz<br>");
document.write("Buzz<br>");
document.write("FIZZBUZZ<br>");
document.write(a + "<br>"); // a not "a"
You’ll notice above that we need to reference the variable a
in order to display the numbers. Your solution is displaying the letter a
instead of the numbers, because you’ve used the string "a"
By using logical OR ( ||
) instead of logical AND ( &&
) you are displaying Fizz for (i) all numbers which are divisible by 3; OR for (ii) numbers which aren’t divisible by both 3 and 5. This then leaves just the numbers which are only divisible by 5 (but not 3 as well) to be displayed as Buzz in the second branch of your conditional execution. So your code outputs a long line of mainly Fizz, with the occasional Buzz, but with no FizzBuzz and no numbers! Have a good think about why this is, and also work out why you need to use logical AND ( &&
) in all your conditions.
Hi Jon,
Thank you for your informative feedback. It is very useful.
Regarding the for loop in FizzBuzz, yes the counter should start with 0 not 1 to get the full 100 iterations. The Atom editor seems to start the count with 1 as default. I need to be aware of this for the future.
Regarding the use of curly braces I agree that we should use current conventions in the way we present code. Programs need to be intuitive for other programmers and not just the author. Unfortunately, I first learned to program 40 years ago so some of my skills might be outdated.
I can totally appreciate that. And programming languages, let alone “conventions”, have certainly undergone a “few” changes during that time And I know from experience in other fields of work, that once you learn one way of doing things it’s much harder to change to a new way than it is for those just learning it for the first time!
Glad you found the comments helpful
Regarding FizzBuzz the posted solution was what I ended up with after looking at the hints.
My first solution looked like this:
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);
}
}
}
}
Hi Yahya,
Your first solution is a very valid alternative
Most people come up with variations on this theme.
Note that you don’t need the excessive nesting of if... else
statements. You can chain the statements by using else if
instead. Here is your same solution, just streamlined with
else if
chaining;for (var i = 1; i <= 100; i++) { // changed < to <= (to include 100)
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);
}
Wow what an extremely thoughtful reply @jon_m
Thank you so much for the advice especially looking at other solutions and reflecting on how they are more concise and improve with more functionality. Also for the big boost in confidence (which is running low haha as I’m tackling the chapter 3 exercises at the moment )
This was very kind and I have taken all this on board. I really appreciate it.
I had a look at your profile and saw that you’re living in Barcelona Yo tambe! Perhaps in the future I can buy you a coffee or beer as a thank you for your help and chat all things crypto and programming. I hope you had a wonderful Sant Joan
, moltes gracies por tots y que vagi be
Hi @jon_m
Thank you so much for the feedback and helping correct were i made mistakes. It really helps me get a better grip of it. But yes this was way to hard for me at this stage but really tried hard to do it from memory. I will keeping practising and with your help and support and also from other community members it really helps me understand more.
Sorry for the late reply, but thank you for pushing me on.
Looping A Triangle:
for(hashtagCount = "#"; hashtagCount.length <= 7; hashtagCount = hashtagCount + "#") {
console.log(hashtagCount);
}
FizzBuzz:
for(num = 1; num <= 100; num++) {
if (num % 3 === 0 && num % 5 === 0) {
console.log("FizzBuzz");
if (num % 3 === 0) {
console.log("Fizz");
}
if (num % 5 === 0) {
console.log("Buzz");
}
} else if (num % 3 === 0) {
console.log("Fizz");
} else if (num % 5 === 0) {
console.log("Buzz");
} else {
console.log(num);
}
}
Chessboard:
var chessBoardSize = 8;
var chessBoard = " ";
var lineBreakCount = 0;
var max = chessBoardSize*chessBoardSize;
for(let a = 1; a < max; a++) {
lineBreakCount++;
if (lineBreakCount === chessBoardSize) {
chessBoard += "\n";
lineBreakCount = 0;
}
if (chessBoard[chessBoard.length - 1] === '\n') {
chessBoard += chessBoard[chessBoard.length - 2];
} else if(chessBoard.slice(-1) === " ") {
chessBoard += '#';
} else {
chessBoard = chessBoard + " ";
}
}
console.log(chessBoard);