How to test a number against two conditions simultaneously with Javascript? - 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();

Related

The output of a fizbuzz function is undefined

I have a Javascript code that is not running in the console in Chrome. It is showing "Undefined" when I call the function.
let output = [];
let count = 1;
function fizzBuzz() {
while (count <= 100) {
if (count % 3 === 0 && count % 5 === 0) {
output.push("FizzBuzz");
} else if (count % 3 === 0) {
output.push("Fizz");
} else if (count % 5 === 0) {
output.push("Buzz");
} else {
output.push(count);
}
count++;
}
}
I am expecting it to output an array of numbers up to 100 but inserting "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both 3 and 5.
You have the following code
let output =[];
let count = 1;
function fizzBuzz() {
while(count<=100) {
if(count % 3 === 0 && count % 5 === 0){
output.push("FizzBuzz");
}else if (count % 3 === 0){
output.push("Fizz");
}else if (count % 5 === 0){
output.push("Buzz");
}else{
output.push(count);
}
count++;
}
}
which you are executing by calling fizzBuzz(). You're getting undefined because when you call fizzBuzz() in the console you're seeing the return value of the execution, which in your code doesn't ever return anything, so we get undefined.
You have two options.
Return output at the end of your function, outside of the while loop. In this case, it'd be best to also move your output and count variables inside, like so
function fizzBuzz() {
let output =[];
let count = 1;
while(count<=100) {
if(count % 3 === 0 && count % 5 === 0){
output.push("FizzBuzz");
}else if (count % 3 === 0){
output.push("Fizz");
}else if (count % 5 === 0){
output.push("Buzz");
}else{
output.push(count);
}
count++;
}
return output;
}
We move the output and count declarations inside so that every time we call fizzBuzz() we start from scratch. If we kept them outside, then if you called fizzBuzz() twice, we'd see the results from the first call in the results from the second call as well.
Your second option is to call fizzBuzz() with how your code currently is, and then print the value of output. I think you're just making a simple mistake and don't realize that your fizzBuzz doesn't return anything.
fizzBuzz();
console.log(output);
or, if you're executing this in your browsers developer console then
fizzBuzz()
output
You are missing a return statement for the output
let output =[];
let count = 1;
function fizzBuzz() {
while(count<=100) {
if(count % 3 === 0 && count % 5 === 0){
output.push("FizzBuzz");
}else if (count % 3 === 0){
output.push("Fizz");
}else if (count % 5 === 0){
output.push("Buzz");
}else{
output.push(count);
}
count++;
}
return output;
}
console.log(fizzBuzz());
If you want run your code without getting undefined then you can use var instead of using 'let'. Due to the variable block scope you are getting undefined.
Just replace let with var.
Then return output variable in your function body. use console.log(your function name)

javascript for loop: issues

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

How to join modulo javascript [Native]

here is my js
while(i <= 25){
if (i % 4 === 0) {
text += 'john';
}
else if (i % 5 === 0) {
text += 'doe'
}
else if ((i % 5 === 0)&&(i % 4 === 0)){do something }
Multiples are both 4 and 5 print 'john doe'
I need to join modulo 5 and 4. Anybody help?
Thank you
while(i <= 25){
if ((i % 5 === 0)&&(i % 4 === 0)) {
do something
}
else if (i % 5 === 0) {
do something
}
else if (i % 4 === 0){ do something }
I hope it will help you.

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.

Javascript Fizzbuzz Issue

I'm trying to do some simple tests to help further my javascript knowledge (which is quite fresh). Goal 1 is to print numbers from 1-100 that aren't divisible by 5 or 3.
I tried the following:
for (var i = 1; i <= 100; i ++)
{
if (i%3 !== 0 || i%5 !== 0){
console.log(i);
}
}
This logs EVERY number from 1-100, and I can't tell why. Probably the simplest simplest questions here but it's doing my head in!
I think you mean &&, not ||. With ||, you're basically testing to see if the number is not divisible by 3 or by 5 - only if a number is divisible by both do you reject it (in other words, multiples of 15).
The typical answer to FizzBuzz is:
if( i%3 == 0 && i%5 == 0) FizzBuzz
elseif( i % 3 == 0) Fizz
elseif( i % 5 == 0) Buzz
else number
So to get directly to the number you need for i%3==0 to be false AND i%5==0 to be false. Therefore, you want if( i%3 !== 0 && i%5 !== 0)
Here's a quite simple FizzBuzz function that accepts a range of numbers.
function fizzBuzz(from, to) {
for(let i = from; i <= to; i++) {
let msg = ''
if(i % 3 == 0) msg += 'Fizz'
if(i % 5 == 0) msg += 'Buzz'
if(msg.length == 0) msg = i
console.log(msg)
}
}
fizzBuzz(1, 25)
As for a more complex solution, that's one way you could define a higher order function which generates customized FizzBuzz functions (with additional divisors and keywords)
function fizzBuzzFactory(keywords) {
return (from, to) => {
for(let i = from; i <= to; i++) {
let msg = ''
Reflect.ownKeys(keywords).forEach((keyword) => {
let divisor = keywords[keyword]
if(i % divisor == 0) msg += keyword
})
if(msg.length == 0) msg = i
console.log(msg)
}
}
}
// generates a new function
const classicFizzBuzz = fizzBuzzFactory({ Fizz: 3, Buzz: 5 })
// accepts a range of numbers
classicFizzBuzz(1, 25)
const extendedFizzBuzz = fizzBuzzFactory({ Fizz: 3, Buzz: 5, Bazz: 7, Fuzz: 11 })
extendedFizzBuzz(1, 25)
I attacked this the same was as Niet the Dark Absol:
for (var n = 1; n <= 100; n++) {
if (n % 3 == 0 && n % 5 == 0)
console.log("FizzBuzz");
else if (n % 3 == 0)
console.log("Fizz");
else if (n % 5 == 0)
console.log("Buzz");
else
console.log(n);
}
However, you can also do it this way:
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0)
output += "Fizz";
if (n % 5 == 0)
output += "Buzz";
console.log(output || n);
}
One of the hardest parts of learning JavaScript - or any language - for me is understanding solutions can come in many ways. I like the first example more, but it's always good to keep thinking and look at other options.

Categories

Resources