I want to find (using JS) the greatest 3-digit number that
leaves a remainder of 1 when divided by 2
leaves a remainder of 2 when divided by 3
leaves a remainder of 3 when divided by 4
leaves a remainder of 4 when divided by 5
This is my code:
var bool = false;
for(var i = 999; bool == true; i = (i - 1)) {
if(i % 2 == 1 && i % 3 == 2 && i % 4 == 3 && i % 5 == 4) {
bool = true;
alert(i);
}
}
But it did not work (somehow there was no error messages, and the alert did not show up). So how can I find that 3-digit number? Thanks.
The loop continuation condition for your loop is bool == true, which is false when the loop starts and so the loop will never execute. Use this instead:
for(var i = 999; i > 0 && !bool; i = (i - 1)) {
or this to strictly obey the "three-digit number" requirement:
for(var i = 999; i >= 100 && !bool; i = (i - 1)) {
I'd also suggest finding a better variable name than bool. found would be appropriate here.
Your breaking condition is wrong here change it to:
Here is Demo
var bool = false;
for(var i = 999; !bool; i--) {
if(i % 2 == 1 && i % 3 == 2 && i % 4 == 3 && i % 5 == 4) {
bool = true;
alert(i);
}
}
Related
Check the divisibility of 2, 3, and both 2 and 3
Divisible by 2 - print x
Divisible by 3 - print y
Divisible by 2 and 3 - print xy
let num = prompt('1-1000');
if (num %2 == 0 )
{
console.log("X")
}
if (num %3 == 0 )
{
console.log("Y")
}
if (num %3 == 0 && num %2 ==0)
{
console.log("XY")
}
Example:
Input: 6
Output:
X
Y
XY
How to make it not print X, Y but XY?
What needs to be done so that several digits to be checked after the decimal point can be entered and then converted to the appropriate letters?
There are a couple of ways you could do this.
One way would be to use a ternary operator to output the values X and/or Y. This will have X logged only when divisible by 2, and Y only logged when divisible by 3. If it is divisible by both, both are shown but their conditions are independent of each other. And using template literals it is concatenated into 1 value.
let num = prompt('1-1000');
console.log(`${(num%2==0)?"X":""}${(num%3==0)?"Y":""}`);
Alternatively, you could use something like what was mentioned in the comments. If you convert the code to a function, you can move the final if statement to the top and use exit/return statements to return only 1 of the values (whichever applies).
const _Check = () => {
let num = prompt('1-1000');
if(num % 3 == 0 && num % 2 == 0) return "XY";
if(num % 2 == 0 ) return "X";
if(num % 3 == 0 ) return "Y";
return "";
}
console.log(_Check());
EDIT
In reponse to a comment by OP: checking for multiple numbers could be done by splitting the value input by the user and then looping through those values.
If the user will be inputting each number separated by a space, you can use the split() method with a space as the separator. And then use forEach() to loop through each number and apply the code/logic we have used earlier.
Example Using ternary operator and template literals:
let num = prompt('1-1000')
num.split(" ").forEach(n => {
console.log(`${(n%2==0)?"X":""}${(n%3==0)?"Y":""}`);
})
Example using a function and return statements:
let num = prompt('1-1000')
const _Check = n => {
if(n %3 == 0 && n %2 ==0) return "XY";
if(n %2 == 0 ) return "X";
if(n %3 == 0 ) return "Y";
return "";
}
num.split(" ").forEach(n => {
console.log(_Check(n));
})
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);
}
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)
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.
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.