javascript for loop: issues - javascript

i came across this problem: 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).
and i tried solving it with the code below:
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 % 5 === 0 && i % 3 === 0) {
console.log("fizzbuzz");
}
console.log(i);
}
please can anyone tell me what i did wrong because i am not getting result

In your condition, i = 15 should be returned fizzbuzz but it returns fizz because 15 can be divided by 3 and 5 so you first condition i % 3 === 0 getting true so it returned fizz. if your first condition is i % 3 === 0 && i % 5 === 0 then i = 15 should be return fizzbuzz.
for(let i = 1; i <= 100; i++){
if (i % 5 === 0 && i % 3 === 0) {
console.log("fizzbuzz");
} else if(i % 3 ===0) {
console.log("fizz");
} else if ( i % 5 === 0 ) {
console.log("buzz");
}
console.log(i);
}

Related

How to print the right name in the code below?

for (var i = 1; i <= 50; i++) {
if (i % 15 === 0 || i % 10 === 0) {
console.log("Donkey!");
} else if (i % 2 !== 0 && (i - 1) % 10 === 0) {
console.log("Monkey!");
} else if (i % 7 === 0) {
continue;
} else {
console.log(i);
}
}
emphasized textIf the number is not divisible by 2 and the previous number is divisible by 10, How do I print Monkey!?
This a very random question with no context but has a rather simple solution. Use modulus check if the remainder of the current number divided by 2 is not 0 and check if the remainder of the last number divided by 10 is 0:
function check(n) {
if (n % 2 !== 0 && (n - 1) % 10 === 0) {
console.log('Monkey!');
}
}
check(11)

How to print a value with the same criteria - JavaScript

I am trying to print numbers that are divisible by 3 and 5.
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.
Tried these codes:
var i;
for(i = 1; i <= 20; 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);
}
}
But it seems it passing out the 3rd else if...
Is there a better way to do this?
You could move the combined comparsion to top of the comparisons, because if both conditions are true, you need not to check the others.
var i;
for (i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) { // check first, includes
console.log("FizzBuzz");
} else if (i % 3 === 0) { // this comparison and
console.log("Fizz");
} else if (i % 5 === 0) { // this as well.
console.log("Buzz");
} else {
console.log(i);
}
}
Less code for same results:
for(var i = 1; i <= 20; i++) {
var output = '';
if(i % 3 === 0) output += 'Fizz';
if(i % 5 === 0) output += 'Buzz';
console.log(output.length > 0 ? output : i);
}
A number divisible by two numbers must be divisible by their Least common multiple, which in this case is 15. You also have to move this check up, so that it's considered before the other statements.
for (var i = 1; i <= 20; i++) {
if (i % 15 == 0)
console.log('FizzBuzz');
else if (i % 3 === 0)
console.log("Fizz");
else if (i % 5 === 0)
console.log("Buzz");
else
console.log(i);
}
var i;
for (i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) {
document.write("FizzBuzz");
} else if (i % 3 === 0) {
document.write("Fizz");
} else if (i % 5 === 0) {
document.write("Buzz");
} else {
document.write(i);
}
}

How to test a number against two conditions simultaneously with Javascript?

I'm having an issue making my loop test against two conditions simultaneously.
for (var result = 0; result <= 100; result ++)
if (result % 3 == 0)
console.log ('Fizz');
else if (result % 5 == 0)
console.log ('Buzz');
else console.log (result);
How can I build a third condition that tests if result is divisible by 3 AND 5?
else if ( result % 5 == 0 && result % 3 == 0)
console.log ('FizzBuzz');
else console.log (result);
This was my solution and 'FizzBuzz' didn't make it the console at all! Why didn't that solution work?
The order matters!
if (result % 5 == 0 && result % 3 == 0)
console.log ('FizzBuzz');
else if (result % 3 == 0)
console.log ('Fizz');
else if (result % 5 == 0)
console.log ('Buzz');
else console.log (result);
When result is both a multiple of 3 AND a multiple of 5, the following both hold:
result is a multiple of 3
result is a multiple of 5
So if you check result % 5 == 0 && result % 3 == 0 in the else part, it will never be true.
You might also consider nested conditionals:
if (result % 3 == 0)
if (result % 5 == 0)
console.log ('FizzBuzz');
else
console.log ('Fizz');
else if (result % 5 == 0)
console.log ('Buzz');
else console.log (result);
Your last code fragment is missing a bit, but I'd assume it's because you're testing your AND statement in an else if block.
You're saying
if result is a multiple of 3 print fizz
otherwise (not a multiple of 3), if result is a multiple of 5 print buzz
otherwise (not a multiple of 3 nor 5), if result is a multiple of 3 and 5 print fizzbuzz
Obviously, result will never be all of the conditions in part 3 since it can't be
NOT 3x
NOR 5y
AND 3x
AND 5y
at the same time
You have the right idea but the wrong order.
Since the single statements:
if(result % x == 0)
will return before the combined if() statement, and so the condition never gets that far. This worked for me:
for(var result=0; result <=15; result++)
if(result % 5 == 0 && result % 3 ==0)
console.log('FizBuzz');
else if(result % 3 == 0)
console.log('Fizz')
else if(result % 5 == 0)
console.log('Buzz');
else
console.log(result)
this is the catch of the task - you don't need to test against (i % 3 === 0 && i % 5 === 0)
function fizzBuzzTest() {
"use strict";
for (let i = 1; i <= 100; i++) {
let str = "";
if (i % 3 === 0) {
str = "Fizz";
}
if (i % 5 === 0) {
str += "Buzz";
}
if (!str) {
str = i;
}
console.log(str);
}
}
fizzBuzzTest();

Problems with Modulo in JavaScript

Doing some practice runs on codecademy and came across the following problem: So the objective is to print "Fizz" if the numbers are divisible by 3. "Buzz" if the numbers are divisible by 5. And "FizzBuzz" if the numbers are divisible by both 3 and 5.
Here is my code, and I thought I had it right, but when I run it they tell me that my code is not 100% accurate. Looking to see any alternatives to this code, or what might be the issue...
Code:
for ( i = 0; i < 21; i++)
{
if (i % 3 == 0 )
{
console.log("Fizz");
}
if (i % 5 == 0)
{
console.log ("Buzz");
}
if ( i % 5 == 0 && i % 3 === 0)
{
console.log("FizzBuzz");
}
else
{
console.log(i);
}
}
You need to use else if to stop from other conditions executing:
for (var i = 1; i <= 20; i++) {
if (i % 5 === 0 && i % 3 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
It can easily be done in a one liner.
for (i = 1; i <= 20; i++) {
console.log(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz'));
};
For a nice formatting also output i on each iteration:
for (i = 1; i < 21; ++i) {
console.log(i+": "+(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz')));
};

Logical Operators in Javascript

I want to print out FizzBuzz when i is both divisible by 3 and by 5. What could be the problem with my code?
for(var i = 1; i<=20; 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);
}
}
If the first or second condition is true, it enters that block, but doesn't evaluate any of the other else if conditions. Because the third condition requires both the first and second to be true, there's no way it will ever enter that block.
Try arranging your conditions like this:
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);
}
}
But just for fun, here's a much more compact version that abuses the conditional operator:
for(var i = 1; i<=20; i++){
console.log(i % 15 ? i % 5 ? i % 3 ? i : "Fizz" : "Buzz" : "FizzBuzz");
}
The main issue is that your check for "FizzBuzz" doesn't happen until after your other comparisons. If i % 3 === 0 (one of the requirements to print "FizzBuzz"), it will never reach the FizzBuzz check.
As a simple fix, move your FizzBuzz check to the first if-statement.
for(var i = 1; i <= 20; i++) {
if(i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
}
else if(i % 5 === 0) {
console.log("Buzz");
}
else if(i % 3 === 0) {
console.log("FizzBuzz");
}
else {
console.log(i);
}
}
As another thing to think about, if i is divisible by both 3 and 5, then it is divisible by their least-common denominator, yes? The least common denominator (the smallest whole number that is divisible by a group of numbers) of 3 and 5 is 15, so you could replace...
if(i % 3 === 0 && i % 5 === 0) {
...with...
if(i % 15 === 0) {
(i%3 ==0 && i%5 ==0) should be the first condition. If you think about it, if i is divisible by 3 and by 5 it will enter the first if statement before it reaches the third.

Categories

Resources