Discussion on JavaScript

I want to know why the 2nd one doesn’t work properly and why 1’st one works properly.
program instruction
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).

Right-Code:(1’st one)


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 );
}

wrong-code:(2’nd one)


for(let i=1;i<=100;i++){
	if(i%3==0){
  	console.log("Fizz");
  }
  else if(i%5==0){
  	console.log("Buzz")
  }
  else if( i%3==0 && i%5==0){
  console.log("FizzBuzz")
  }
  else{
  console.log(i)
  }
  
}


The problem is in the ordering of your conditionals.

You have this third:

  else if( i%3==0 && i%5==0){
  console.log("FizzBuzz")
  }

But remember that an else only is evaluated if all the previous conditions were false. If the number is divisible by 3 it will already have gone through the first condition block and none of the elses will be looked at.

1 Like

In the 1’st one it don’t mention about my third condition but it still show “FizzBuzz” which is divided by both 3 and 5. For Example , number 15 shows the “FizzBuzz” output. That’s the confusion for me.

The first code block is using two if statements. Notice that the second one does not have an else. That means that the second condition will be checked whether or not the first one is true.

So the first code snippet is 2 separate if blocks. Every time the code run, both block gets executed.

2nd code snippet is one large if…else block. So only either the if block or one of the else blocks gets executed, not all of them.

If I change the 2’nd one like this, is it solve the problem?

for(let i=1;i<=100;i++){
	if(i%3==0){
  	console.log("Fizz");
  }
  if(i%5==0){
  	console.log("Buzz")
  }
  if( i%3==0 && i%5==0){
  console.log("FizzBuzz")
  }
  else{
  console.log(i)
  }
  
}

Well, that will print FizzBuzz twice if you have one divisible by both.

Edit. Actually it would print

Fizz
Buzz
FizzBuzz

1 Like

For a number like 15, that is divisble by both 3 and 5, your code will log three times: “Fizz”, “Buzz”, and “FizzBuzz”.

That’s the question of mine as well. How can I fix this?

Its actually simpler than you think. The 2nd code snippet isn’t wrong per se, but which condition do you think you should check first?

Take the number 15 and walk through your code line by line once.

I just want that the number which is divided by both 3 and 5 should print only " FizzBuzz", not ‘Fizz’ , ‘Buzz’ , ‘FizzBuzz’. I want the correct output from the 2’nd one.

Well, we’ve discussed the significance of the word else. If you want certain code to execute in one situation and want different code to execute only when that situation is not true, then you can do this using if and else in the correct order.

1 Like

Don’t get me wrong. I just want to clear my confusion. I am not that much knowledgeable about JavaScript. Just trying to learn it.

I want to refrain from actually just giving you the code. This isn’t a JavaScript problem, it’s just logics. Step through your solution and find the logic.

We’ve given you all the information you need. We told that if else control structure execute only the first block that meets the condition. Based on that how do you think you can get the code to print FizzBuzz and not Fizz, or Buzz or Fizz, Buzz, FizzBuzz.

Thanks a lot all of you. I got the point.

Sometimes I got stuck on silly issues.

It is actually a simple fix that you need. Can you figure it out if I comment your code like this?

for(let i=1;i<=100;i++){
        // when i = 15
	if(i%3==0){  /* this is true so the code inside it is executed */
  	console.log("Fizz");
  }  // this others are not executed
  else if(i%5==0){   
  	console.log("Buzz")   // this is never reached
  }
  else if( i%3==0 && i%5==0){
  console.log("FizzBuzz")  // this is never reached - but you want this one
  } 
  else{  // this is never reached
  console.log(i)  
  }
  
}

Thanks @ Ieahleen for your co-operation. I got the point. I am a little bit sluggish in understanding.

1 Like

You’re not sluggish. We’ve been trying to give you the smallest amount of information at a time that we can so that you’re brain is doing the hard work of connecting the dots.

1 Like

Getting the logic of statement blocks can be very very confusing. It isn’t a matter of you being sluggish, it’s a matter of how we think. We tend to see what we want, and present a way that makes sense to us. But remember: javascript, for all it’s coolth, is stupid. Stupider than a retarded squirrel with brainfreeze AND a lobotomy. Javascript can only do what you tell it, exactly as you tell it. Nothing more. But we think in bigger picture thoughts.

I find myself going back to pseudocode more and more lately. I will simply put aside the programming bit, and look at the logic without language. What are the steps I need to handle? What are the possible cases? How can I flow from one step to the next? Define a program flow, without worrying about what type of variable to use,or what looping mechanism will work best. Simply map out the data flow.

In this one, the logic might have gone:

if a number is divisible by both 5 and 3, log "FizzBuzz"
else
  // Here it is not divisible by BOTH -- but it might be by ONE.
  if a number is divisible by 5, log "Fizz"
  else if a number is divisible by 3, log "Buzz"
  else log the number
end if

I get stuck in the mire sometimes, too, but having a logic map sometimes helps. :wink:

1 Like