How to join modulo javascript [Native] - javascript

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.

Related

FizzBuzz: num%15 being first versus num%3 being first

I just want to know why 'FizzBuzz only works when num % 15 === 0 is first as opposed to when num % 3 === 0 is first.
For example:
for (let num = 1; num <= 100; num++) {
if (num % 15 === 0) {
console.log('FizzBuzz');
} else if (num % 5 === 0) {
console.log('Buzz')
} else if (num % 3 === 0) {
console.log('Fizz');
} else {
console.log(num);
}
}
vs.
for (let num = 1; num <= 100; num++) {
if (num % 3 === 0) {
console.log('Fizz');
} else if (num % 5 === 0) {
console.log('Buzz')
} else if (num % 15 === 0) {
console.log('FizzBuzz');
} else {
console.log(num);
}
}
else if basically means "If the previous condition didn't pass THEN IF...". So, in your second snippet, if num % 3 === 0 is true, it will never bother checking if num % 5 === 0 or num % 15 === 0.

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

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.

checking two different numbers modulos to 0 at the same time(Javascript)

I'm trying to get a program to print out a console log statement if a number 1 - 20 is divisible by 3, 5, or both. This is what I'm using, but it wont check both numbers....
for (i = 1; i < 21 ; i++) {
if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if ((i % 5)&&(i % 3) === 0) {
console.log("FizzBuzz");
} else {
console.log(i);
}
}
I tried searching for this, but I think my search criteria was not well stated.
Thank you guys!
You've got a typo in the third comparison. It should be this:
else if ((i % 5) === 0 &&(i % 3) === 0)
Also, that comparison has to be first, otherwise it gets short-circuited by the other two.
if (i % 5 === 0 && i % 3 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} // ...
Fiddle
first check both
for (i = 1; i < 21 ; 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);
}
}
for (i = 1; i < 21 ; i++) {
if ((i % 5)==0 && (i % 3) == 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("FizzBuzz");
} else {
console.log(i);
}
The one that sees if both are applicable needs to be first, otherwise it will never be met.
And you need to evaluate that condition as: (i % 5) == 0 && (i % 3) == 0
There are two issues:
Checking (x && y) === 0 is not the same as checking (x === 0) && (y === 0)
You have to put the third condition first, otherwise it will never be reached
Code:
for (i = 1; i < 21 ; 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);
}
}

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