I've encountered some misundertanding. There is a for cycle with some if statements:
for (var number = 1; number < 100; number++) {
if (number % 3 == 0 && number % 5 == 0)
console.log(number + "fizzbuzz");
if (number % 5 == 0)
console.log(number + " buzz");
if (number % 3 == 0)
console.log(number + " fizz");
else console.log(number);
}
The output of this code is 1, 2, 3 fizz, 4, 5 buzz, etc. So it's what expected.
But if we delete braces the output will be like this:
15fizzbuzz
30fizzbuzz
45fizzbuzz
60fizzbuzz
75fizzbuzz
90fizzbuzz
100 buzz
100
Also, there is a second implementation of this program(with the right-way if-else statements):
for (var number = 1; number < 100; number++)
if (number % 3 == 0 && number % 5 == 0)
console.log(number + "fizzbuzz");
else if (number % 5 == 0)
console.log(number + "buzz");
else if (number % 3 == 0)
console.log(number + "fizz");
else console.log(number);
Notice that there are no braces too, but the output is ok.
Can you explain, what's the difference?
When you miss a semicolon or brackets, javascript tries to insert it on its own, & at times can produce some weird results like this. (Which is correct by the rules, just humans & machine don't agree on how to process it :D )
When you remove braces of for loop javascript tries to puts braces in code & run it, this is different that how you expect it to behave thus you are confused!
What you wrote & read:
for (var number = 1; number < 100; number++)
if (number % 3 == 0 && number % 5 == 0)
console.log(number + "fizzbuzz");
if (number % 5 == 0)
console.log(number + " buzz");
if (number % 3 == 0)
console.log(number + " fizz");
else console.log(number);
What javascript did with it & executes:
for (var number = 1; number < 100; number++){ //runs loop here
if (number % 3 == 0 && number % 5 == 0){
console.log(number + "fizzbuzz"); //prints for first condition
}
}
//now number is 100!
if (number % 5 == 0){
console.log(number + " buzz"); //prints for second condition once cause 100%5==0 is true
}
if (number % 3 == 0){
console.log(number + " fizz");
}
else{
console.log(number); //prints for this else condition once cause 100%3==0 is false
}
Which is perfectly valid & there is no error or bug here!
This happens because if the is no immediate else after if then javascript terminate that statement there, but if you use else...if then it continues that statement till if find a else or a statement not followed by else
If you want to play with this type of behaviour use Google Closure Compiler to see how code is interpreted by machine.
NOTE: As #carcigenicate suggest in comments, Always use braces!
As a lot of comments pointed out, its the lack of elses ( or blocks ) in your first code that make it going wrong.
//a bit shortified to make it clearer
var a=true,b=true;
if(a && b){ }// will be executed
if(a){ } //will be executed
if(b){} //will be executed
//vs.
if(a&&b){}//will be executed
else if(a){}//else => not executed
else if(b){}//else => not executed
However, it might be better to restructure your code as its quite repetitive:
for (var number = 1; number < 100; number++)
console.log(number+ (number % 3 == 0?"fizz":"")+ (number % 5 == 0?"buzz":""));
So log the number, if its a multiple of 3 add "fizz" and if its an multiple of 5 add "buzz"...
The syntax of a for loop is for (statementA; statementB; statementC) statementD. Statements can be grouped together with {}, so {statement, statement, ...} can be used where a single statement is expected.
for (var number = 1; number < 100; number++) {
if (number % 3 == 0 && number % 5 == 0)
console.log(number + "fizzbuzz");
if (number % 5 == 0)
console.log(number + " buzz");
In this case statementA is var number = 1, statementB is number < 100, and statementC is number++ and statementD is if (number % 3 == 0 && number % 5 == 0) console.log(number + "fizzbuzz"). The second if is another statement that does not belong to the for loop. If you want for the second if statement to belong to the for loop, you need to use {} to group the statements together.
The syntax of a if statement is if (expression) statement or if (expression) else statement. Using several if else aligned you are able to pass the first statement to the for loop, the second statement is going to belong to the first if that still belongs to the for loop. That is why the last example works without {}.
It is important to note that the code may work, but it is still bad code. It is recommended to use {} to group the statement from below for for, while, and if even if it is a single statement.
You may want to learn the JavaScript syntax before trying to understand JavaScript code. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps
Related
I've made a sequence of numbers from 0 to 20 and I want to change the sequence so once it comes up with a multiple of 3 and 5 it logs 'FizzBuzz' to the terminal then carries on with the rest of the numbers up to 20. My problem is once I have changed the number to the string the rest of the terms in the sequence come up with NaN. I know the problem with my code is that I'm changing the number to a string and you cannot perform addition to a string which is why NaN comes up. I'm pretty new to this so any thoughts on how to do complete this would be greatly appreciated. I've tried using .append() but I'm pretty sure I'm using it incorrectly.
My code thus far;
var increment = function(number)
{
for (var i = 1; i <= 20; i++)
{
console.log(number++)
if ((number % 3 === 0) && (number % 5 === 0))
{
number = "FizzBuzz"
console.log("FizzBuzz");
}
else if (number % 3 === 0)
{
console.log("Fizz");
}
else if (number % 5 === 0)
{
console.log("Buzz");
}
else
{}
}
}
increment(1)
When you find a multiple of 3 and 5, you are setting number to "FizzBuzz", which does not have a ++ operator. On the next iteration, you call ++ on number, which is now "FizzBuzz", so it logs NaN.
If you don't set number to "FizzBuzz" it should work fine.
This would do it
var increment = function()
{
for (var i = 1; i <= 20; 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)
}
}
}
increment()
There's no need to pass the number parameter, since your for loop is already set to increment by 1 (i++).
So what i need to do in this task is to print out the numbers from 1-20. And the code should also fulfill the rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the
console.
Otherwise, just print out the number.
for ( var i = 0 ; i < 20 ; i++) {
if ( i % 3) {
console.log("Fizz");
}
else if( i % 5) {
console.log("Buzz");
}
else if(i % 3 || 5) {
console.log("FizzBuzz");
}
else {
console.log(i);
}
}
The error i am getting :"You printed FizzBuzz when you should have printed 1"
You have some errors in your code
for (var i = 1; i < 21; i++) { // needs to start with 1
// You should check this condition first
if (i % 3 == 0 && i % 5 == 0) { //needs '==' and '&&' operator
console.log("FizzBuzz");
} else if (i % 3 == 0) { // you need to check for equality to zero
console.log("Fizz");
} else if (i % 5 == 0) { // here too, needs '=='
console.log("Buzz");
} else {
console.log(i);
}
}
A one line solution
for(i=1;i<=20;i++)console.log((!(i%3)?'Fizz':'')+(!(i%5)?'Buzz':'') || i);
Tests for matches using two conditional (ternary) operators. The results are concatenated, eliminating the need for a third "matches both" test. A logical OR is used to print the index when the string result is empty, i.e., no matches. A single console statement outputs the final result.
As for the errors in OP's code, #elclanrs already pointed out the problem in a comment. else if(i % 3 || 5) is incorrect (suggest printing the result in the console to see why).
Run the snippet to try
// Here we output to the screen rather than the console
for(i=1;i<=20;i++)window.stdout.innerHTML+='<li>'+((!(i%3)?'Fizz':'')+(!(i%5)?'Buzz':'')||i);
<ol id="stdout">
Write a javascript program that displays the numbers from 10 to 100. But for multiples of 4 print "Penny" instead of the number and for multiples of 6 print "Leonard". For numbers which are multiples of both 4 and 6 print "Bazzinga"
I know how to do two parts struggling to print 6 and 4;
function baZzinga (number) {
for (var number = 10; number <= 101; number++)
if(number % 4 == 0) {
console.log("penny");
}
else if (number % 6 == 0) {
console.log("Leonard");
} else if ( not sure what goes here) {
help help help
} else {
console.log(number");
}
You want the and condition first. Try this
var result = document.getElementById("result");
function baZzinga (number) {
for (var number = 10; number <= 101; number++) {
if (number % 4 == 0 && number % 6 == 0) {
result.innerHTML += "Bazinga";
}
else if(number % 4 == 0) {
result.innerHTML += "penny";
}
else if (number % 6 == 0) {
result.innerHTML += "Leonard";
}
else {
result.innerHTML += number;
}
}
}
baZzinga()
<p id="result"></p>
I changed console.log to result.innerHTML because I wanted to demonstrate it in a snippet.
I have a few comments on your code -- constructive criticism, I hope! First, you don't need the number parameter in your bazzinga function. Next, the indentation of the code you posted makes it hard to read. Finally, you should almost always use === instead of ==. The === tests for strict equality, whereas == tries to do some type conversions first (and can therefore produce unexpected results). See the official docs.
To answer you question: check for divisibility by 6 AND 8 first. That way, it will override the individual cases. I believe you want something like this:
function bazzinga() {
for (var number = 10; number <= 100; number++) {
if (number % 4 === 0 && number % 6 === 0) {
console.log("Bazzinga");
} else if (number % 4 === 0) {
console.log("Penny");
} else if (number % 6 === 0) {
console.log("Leonard");
}
}
}
Here is a solution using the format you posted:
for (var number = 10; number <= 100; number++) {
if(number % 4 === 0 && number % 6 === 0){
console.log("bazzinga");
} else if(number % 4 === 0) {
console.log("penny");
} else if (number % 6 === 0) {
console.log("Leonard");
} else {
console.log(number);
}
}
Or use the ternary operator to be even more succinct!
for (var i = 10; i <= 100; i++){
var penny = i % 4 === 0;
var leonard = i % 6 === 0;
console.log(penny ? (leonard ? "bazzinga" : "penny"): leonard ? "leonard" : i);
}
function process_num(num) {
return num % 4 == 0 ? num % 6 == 0 ? "Bazzinga" : "Penny" : num % 6 == 0 ? "Leonard" : num;
}
for (x = 10; x <= 100; x++) { console.log( x + ': is ', process_num(x)) }
Nested Ternary operator for conciseness
If it passes outer ternary test it is divisible by 4:
Enter into nested termary one to test if num is also divisible by 6 for the BaZzinga prize!!
If it fails the BaZzinga challenge, we know it previously passed the divisible by 4 test so print "penny"
Failing the outer ternary condition, we know it's not divisible by 4:
Enter nested ternary two to consider if divisible by 6. If so print "Leonard".
If not it's failed both the outer (div by 4) and inner (div by 6) so return the number unchanged.
Now that the logic is contained in the function, we can just create a for loop to iterate over the required numbers printing out the correct values.
I just figured out how to test for certain conditions and modify output within a loop. But I noticed that testing for two conditionals with the && operator only works in an if/else if/else if/else chain if it's the first one tested for.
Can someone explain why this works:
var number = 0;
var counter = 0;
while (counter < 100) {
number ++;
counter ++;
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);
}
But this does not?:
var number = 0;
var counter = 0;
while (counter < 100) {
number ++;
counter ++;
if (number % 3 == 0)
console.log("Fizz");
else if (number % 5 == 0)
console.log("Buzz");
else if (number % 3 == 0 && number % 5 == 0)
console.log ("FizzBuzz");
else
console.log(number);
}
An else if, as the name suggests, will only execute when a previous if fails. So the statement else if (number % 3 == 0 && number % 5 == 0) will execute only when if (number % 3 == 0) and else if (number % 5 == 0) fail. If a number is a multiple of 3 and 5 both, then the first if gets successfully executed, and the rest ifs and else-ifs are ignored.
However, in code 1, the ordering of ifs and else-ifs is such that, if a number is divisible by both 3 & 5, then first if is executed, if it is divisible by the only 3, then first if is not executed, only else if (number % 3 == 0) is executed.
Let's make an example using the numbers 6, 10, 15.
The number 6 will execute - in your first example (the working example) - the second if block because in the first one the condition will not be satisfied while the third and fourth block will be ignored, and - in your second example (the not-working example) - will execute the first if block and ignore the other blocks that follow.
The number 10 will execute - in your first example - the third block because the first's and second's condition is not satisfied while the fourth block will be ignored, and - in your second example - will execute the second block, because the condition in the first block is not satisfied, while the blocks that follow will be ignored.
The number 15 will execute - in your first example - the first block and ignore the blocks that follow, and - in your second example - will also execute the first block because the condition is satisfied while the blocks that follow will be ignored.
So, to recap, in your second example, the third if block will never be executed because the condition for its execution is made up of an and of the first and second if block's conditions. In order for the third block to be executed you would need a case where the first if block's condition (let's say c1) and the second if block's condition (let's say c2) are false and c1 && c2 is true, but in order to have c1 && c2 to true you need c1 and c2 to be true, which leads to the execution of the first block and skipping of the rest.
You want to test for if the the number is divisible by three and five, but before you do that you test if it is just divisible by three.
It is, so it follows that branch of logic and never attempts to test if it is divisible by three and five.
Because in your test if the number is a multiple of 3 or 5 then the corresponding if statemetn will get executed before the number % 3 == 0 && number % 5 == 0 statement is reached so it will never get executed.
Let us assume the number is 33, the the first test will become success which is correct, but if the number if 15 then again the first if is success because 15 is a multiple of 3 so even though it is a multiple of 5 also the 3rd condition will not get a chance to execute
To get it correct you may need something like below, where if the number is a multiple of both the versions we skip first 2 conditions
var number = 0;
var counter = 0;
while (counter < 100) {
number++;
counter++;
if (number % 3 == 0 && number % 5 != 0) {
console.log("Fizz");
} else if (number % 5 == 0 && number % 3 != 0) {
console.log("Buzz");
} else if (number % 3 == 0 && number % 5 == 0) {
console.log("FizzBuzz");
} else {
console.log(number);
}
}
Everything that is either evenly divisible by 3 or evenly divisible by 5 has been removed in the second version. By the time it checks to see if a number is divisible by 3 and divisible by 5 there is no chance of it being true because one of the first two clauses already evaluated to be true.
Consider this pseudo code
if( A || B ) return;
if( A && B ) //this code will never execute
and then consider A to be number % 3 == 0 and B to be number % 5 == 0. This is essentially what is happening, and why the last if statement never executes.
What you actually want to test is
if (number % 3 == 0 && number % 5 == 0) …
else if (number % 3 == 0 && number % 5 != 0) …
else if (number % 3 != 0 && number % 5 == 0) …
else if (number % 3 != 0 && number % 5 != 0) …
if you'd write out the four cases.
Only you don't need to be that explicit, because when the previous conditions already did not match (and you are in the else branch), then those != 0 are implied and you can omit them. However, order matters, as the conditions are tested consecutively.
So if you have the fully qualified conditions, you can shuffle their order as you want:
if (number % 3 == 0 && number % 5 != 0) … // move to front
else if (number % 3 != 0 && number % 5 == 0) …
else if (number % 3 == 0 && number % 5 == 0) …
else if (number % 3 != 0 && number % 5 != 0) …
and then continue to simplify conditions, omitting the parts that are already implied by their parent cases:
if (number % 3 == 0 && number % 5 != 0)
console.log("Fizz");
else if (number % 3 != 0 && number % 5 == 0) // (an == instead of the && would suffice)
console.log("Buzz");
else if (number % 3 == 0) // as it didn't match the first condition, we know that % 5 == 0
console.log("FizzBuzz");
else // here we know that % 3 != 0 && % 5 != 0
console.log(numer);
Other permutations of the condition let us use as few as in your original example, like
if (number % 3 == 0 && number % 5 != 0)
console.log("Fizz");
else if (number % 3 == 0) // as it didn't match the first condition, we know that % 5 == 0
console.log("FizzBuzz");
else if (number % 5 == 0) // as it didn't match the first condition, we know that % 3 != 0
console.log("Buzz");
else // here we know that % 3 != 0 && % 5 != 0
console.log(numer);
And the minimum number of tests would be achievable by nesting them:
if (number % 3 == 0)
if (number % 5 == 0)
console.log("FizzBuzz");
else
console.log("Fizz");
else
if (number % 5 == 0)
console.log("Buzz");
else
console.log(numer);
I am trying to iterate through the arrays in the numbers variable, and if a number can be divided by 3 I'm logging "fizz", if it can be divided by 5 I'm logging "buzz", and if a number can be divided by 3 + 5, or 15, I'm logging "fizzbuzz"
Here is the working code:
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <= numbers.length; i++) {
if (i % 15 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else {
console.log(i);
}
};
Here is my original code, which doesn't log "fizzbuzz"
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <= numbers.length; i++) {
if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 15 === 0) {
console.log("FizzBuzz");
} else {
console.log(i);
}
};
Why does the (i % 15 === 0) condition need to precede the other two conditions? Shouldn't it not matter?
The number 15 is divisible by both 3 and 5. If you don't test it first, then you'll never get there.
So let's take 30 as an example. If you check 15 first, you'll see that it's divisible by 15. However, if you check either 5 or 3 first, it'll be flagged as being divisible by either of those.
After one of your conditions evaluates as true, you break out of that if-block and don't evaluate following else-if/else statements. If you want the rest of them to evaluate you can make them if statements instead of else-if's.
It's because firstly the computer checks for the first statement and then the others as you use the ELSE IF which means, to check the statement after the first one is false. Use IF for all instead.