How to print the right name in the code below? - javascript

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)

Related

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

Javascript IIFE changes result

I'm looking at projecteuler.net's 4th problem, and have come across a curious feature that I'm wondering if anyone could explain.
The following code returns 10001
var n = 999 * 999; //biggest product with 3 digit numbers
var x;
while (n>10000) { //smallest product of 3 digit numbers
if (n.toString() === n.toString().split('').reverse().join('')) {
x = Math.floor(Math.sqrt(n));
while (n % x !== 0 && x >= 100 && n/x <= 999) {
x--;
}
if (n % x === 0 && x>= 100 && n/x <= 999) {
n;
}
}
n--;
}
whereas when wrapped in an IIFE, it returns 906609 which is the correct answer.
(function euler4() {
var n = 999 * 999; //biggest product with 3 digit numbers
var x;
while (n>10000) { //smallest product of 3 digit numbers
if (n.toString() === n.toString().split('').reverse().join('')) {
x = Math.floor(Math.sqrt(n));
while (n % x !== 0 && x >= 100 && n/x <= 999) {
x--;
}
if (n % x === 0 && x>= 100 && n/x <= 999) {
return n;
}
}
n--;
}
}());
Does anybody know why? I can't find an explanation online. Cheers!
The lone n in the first does not terminate the algorithm, whereas the return n in the second does. This can be fixed by replacing n in the first with a simple break
var n = 999 * 999; //biggest product with 3 digit numbers
var x;
while (n>10000) { //smallest product of 3 digit numbers
if (n.toString() === n.toString().split('').reverse().join('')) {
x = Math.floor(Math.sqrt(n));
while (n % x !== 0 && x >= 100 && n/x <= 999) {
x--;
}
if (n % x === 0 && x>= 100 && n/x <= 999) {
break;
}
}
n--;
}
console.log(n);

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