FizzBuzz: 15 shows Fizz but no Buzz on newline. Why? Just need clarification
Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.
Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use
if ((int%3 == 0) && (int%5 == 0))
either at the end or the beginning of the block is when the changes are visible.
Note:
I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.
My solution:
for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/
else{
console.log(int);
}
}
javascript fizzbuzz
add a comment |
Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.
Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use
if ((int%3 == 0) && (int%5 == 0))
either at the end or the beginning of the block is when the changes are visible.
Note:
I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.
My solution:
for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/
else{
console.log(int);
}
}
javascript fizzbuzz
Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)
– CertainPerformance
Jan 1 at 9:55
1
if
/else if
/… are processed in the order you write them. In the quoted code ifint$3 == 0
is true the first branch is taken and all the others are skipped. But whenint
is 15 thenint$3 == 0
is true. You need to put the most specific cases first.
– Richard
Jan 1 at 9:59
@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.
– kashfil_aziz
Jan 1 at 10:02
@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.
– kashfil_aziz
Jan 1 at 10:12
add a comment |
Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.
Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use
if ((int%3 == 0) && (int%5 == 0))
either at the end or the beginning of the block is when the changes are visible.
Note:
I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.
My solution:
for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/
else{
console.log(int);
}
}
javascript fizzbuzz
Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.
Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use
if ((int%3 == 0) && (int%5 == 0))
either at the end or the beginning of the block is when the changes are visible.
Note:
I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.
My solution:
for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/
else{
console.log(int);
}
}
javascript fizzbuzz
javascript fizzbuzz
edited Jan 1 at 10:10
kashfil_aziz
asked Jan 1 at 9:53
kashfil_azizkashfil_aziz
126
126
Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)
– CertainPerformance
Jan 1 at 9:55
1
if
/else if
/… are processed in the order you write them. In the quoted code ifint$3 == 0
is true the first branch is taken and all the others are skipped. But whenint
is 15 thenint$3 == 0
is true. You need to put the most specific cases first.
– Richard
Jan 1 at 9:59
@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.
– kashfil_aziz
Jan 1 at 10:02
@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.
– kashfil_aziz
Jan 1 at 10:12
add a comment |
Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)
– CertainPerformance
Jan 1 at 9:55
1
if
/else if
/… are processed in the order you write them. In the quoted code ifint$3 == 0
is true the first branch is taken and all the others are skipped. But whenint
is 15 thenint$3 == 0
is true. You need to put the most specific cases first.
– Richard
Jan 1 at 9:59
@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.
– kashfil_aziz
Jan 1 at 10:02
@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.
– kashfil_aziz
Jan 1 at 10:12
Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)
– CertainPerformance
Jan 1 at 9:55
Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)
– CertainPerformance
Jan 1 at 9:55
1
1
if
/else if
/… are processed in the order you write them. In the quoted code if int$3 == 0
is true the first branch is taken and all the others are skipped. But when int
is 15 then int$3 == 0
is true. You need to put the most specific cases first.– Richard
Jan 1 at 9:59
if
/else if
/… are processed in the order you write them. In the quoted code if int$3 == 0
is true the first branch is taken and all the others are skipped. But when int
is 15 then int$3 == 0
is true. You need to put the most specific cases first.– Richard
Jan 1 at 9:59
@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.
– kashfil_aziz
Jan 1 at 10:02
@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.
– kashfil_aziz
Jan 1 at 10:02
@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.
– kashfil_aziz
Jan 1 at 10:12
@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.
– kashfil_aziz
Jan 1 at 10:12
add a comment |
4 Answers
4
active
oldest
votes
In you solution, the following block is dead code :
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
This console.log('Fizz'+'Buzz')
can never be reached because ((int%3 == 0) && (int%5 == 0))
would mean that (int%3 == 0)
and so the first if
is executed. Because of the meaning of else if
, this later code block is never reached.
So to answer directly :
show their 15 appearing with Fizz but Buzz is on a newline
This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.
my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.
Because else if
blocks order is important, and you chose the wrong one.
Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.
– kashfil_aziz
Jan 1 at 10:15
add a comment |
If you remove else
from else if(int%5 == 0)
you will get your desired output I guess.
add a comment |
You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for
if(int%3 == 0){
console.log('Fizz');
}
And it will never reach the other if statements.
add a comment |
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
Firstly change comparisons into strict equality ===
it will help you to evade problems in your code;
Then put it on top of your comparisons, it must look like following code:
for(let int = 1; int <= 100; int++) {
if ( i % 3 === 0 && i % 5 === 0 ) {}
We had to put our most specific match at the top and then filter down our checks to most general.
You can even wrap it into function so you can call it and reuse;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994501%2ffizzbuzz-15-shows-fizz-but-no-buzz-on-newline-why-just-need-clarification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
In you solution, the following block is dead code :
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
This console.log('Fizz'+'Buzz')
can never be reached because ((int%3 == 0) && (int%5 == 0))
would mean that (int%3 == 0)
and so the first if
is executed. Because of the meaning of else if
, this later code block is never reached.
So to answer directly :
show their 15 appearing with Fizz but Buzz is on a newline
This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.
my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.
Because else if
blocks order is important, and you chose the wrong one.
Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.
– kashfil_aziz
Jan 1 at 10:15
add a comment |
In you solution, the following block is dead code :
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
This console.log('Fizz'+'Buzz')
can never be reached because ((int%3 == 0) && (int%5 == 0))
would mean that (int%3 == 0)
and so the first if
is executed. Because of the meaning of else if
, this later code block is never reached.
So to answer directly :
show their 15 appearing with Fizz but Buzz is on a newline
This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.
my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.
Because else if
blocks order is important, and you chose the wrong one.
Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.
– kashfil_aziz
Jan 1 at 10:15
add a comment |
In you solution, the following block is dead code :
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
This console.log('Fizz'+'Buzz')
can never be reached because ((int%3 == 0) && (int%5 == 0))
would mean that (int%3 == 0)
and so the first if
is executed. Because of the meaning of else if
, this later code block is never reached.
So to answer directly :
show their 15 appearing with Fizz but Buzz is on a newline
This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.
my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.
Because else if
blocks order is important, and you chose the wrong one.
In you solution, the following block is dead code :
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
This console.log('Fizz'+'Buzz')
can never be reached because ((int%3 == 0) && (int%5 == 0))
would mean that (int%3 == 0)
and so the first if
is executed. Because of the meaning of else if
, this later code block is never reached.
So to answer directly :
show their 15 appearing with Fizz but Buzz is on a newline
This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.
my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.
Because else if
blocks order is important, and you chose the wrong one.
answered Jan 1 at 10:10
SuricatSuricat
1214
1214
Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.
– kashfil_aziz
Jan 1 at 10:15
add a comment |
Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.
– kashfil_aziz
Jan 1 at 10:15
Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.
– kashfil_aziz
Jan 1 at 10:15
Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.
– kashfil_aziz
Jan 1 at 10:15
add a comment |
If you remove else
from else if(int%5 == 0)
you will get your desired output I guess.
add a comment |
If you remove else
from else if(int%5 == 0)
you will get your desired output I guess.
add a comment |
If you remove else
from else if(int%5 == 0)
you will get your desired output I guess.
If you remove else
from else if(int%5 == 0)
you will get your desired output I guess.
answered Jan 1 at 10:00


Partho63Partho63
1,8521923
1,8521923
add a comment |
add a comment |
You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for
if(int%3 == 0){
console.log('Fizz');
}
And it will never reach the other if statements.
add a comment |
You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for
if(int%3 == 0){
console.log('Fizz');
}
And it will never reach the other if statements.
add a comment |
You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for
if(int%3 == 0){
console.log('Fizz');
}
And it will never reach the other if statements.
You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for
if(int%3 == 0){
console.log('Fizz');
}
And it will never reach the other if statements.
answered Jan 1 at 10:11
KingVKingV
373
373
add a comment |
add a comment |
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
Firstly change comparisons into strict equality ===
it will help you to evade problems in your code;
Then put it on top of your comparisons, it must look like following code:
for(let int = 1; int <= 100; int++) {
if ( i % 3 === 0 && i % 5 === 0 ) {}
We had to put our most specific match at the top and then filter down our checks to most general.
You can even wrap it into function so you can call it and reuse;
add a comment |
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
Firstly change comparisons into strict equality ===
it will help you to evade problems in your code;
Then put it on top of your comparisons, it must look like following code:
for(let int = 1; int <= 100; int++) {
if ( i % 3 === 0 && i % 5 === 0 ) {}
We had to put our most specific match at the top and then filter down our checks to most general.
You can even wrap it into function so you can call it and reuse;
add a comment |
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
Firstly change comparisons into strict equality ===
it will help you to evade problems in your code;
Then put it on top of your comparisons, it must look like following code:
for(let int = 1; int <= 100; int++) {
if ( i % 3 === 0 && i % 5 === 0 ) {}
We had to put our most specific match at the top and then filter down our checks to most general.
You can even wrap it into function so you can call it and reuse;
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
Firstly change comparisons into strict equality ===
it will help you to evade problems in your code;
Then put it on top of your comparisons, it must look like following code:
for(let int = 1; int <= 100; int++) {
if ( i % 3 === 0 && i % 5 === 0 ) {}
We had to put our most specific match at the top and then filter down our checks to most general.
You can even wrap it into function so you can call it and reuse;
answered Jan 1 at 10:35


Max KazionovMax Kazionov
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994501%2ffizzbuzz-15-shows-fizz-but-no-buzz-on-newline-why-just-need-clarification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)
– CertainPerformance
Jan 1 at 9:55
1
if
/else if
/… are processed in the order you write them. In the quoted code ifint$3 == 0
is true the first branch is taken and all the others are skipped. But whenint
is 15 thenint$3 == 0
is true. You need to put the most specific cases first.– Richard
Jan 1 at 9:59
@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.
– kashfil_aziz
Jan 1 at 10:02
@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.
– kashfil_aziz
Jan 1 at 10:12