Else/If Loop JavaScript - javascript

I'm really novice to all of this, and I'm learning it in class. My assignment is to write an Else/If Loop where we display 1-100. If the integer is divisible by 3 display "play", if divisible by 4 display "ball", and if divisible by 3 and 4 display "Play Ball", anything else is just the integer.
I have my code working here, but I can't get my 3 and 4 to display "Play Ball", unless I run it by itself. Hope that makes sense, here's what I have:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0) {
console.log('Play');
} else if (i % 4 === 0) {
console.log('Ball');
} else if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else {
console.log(i);
}
}

You just need to move thisi % 3 === 0 && i % 4 === 0 condition to the top:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else if (i % 3 === 0) {
console.log('Play');
} else if (i % 4 === 0) {
console.log('Ball');
} else {
console.log(i);
}
}

Since the first if statement will always be true when the third statement (with the &&) will be true, you'll never make it to the third statement. Try reordering as such:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else if (i % 4 === 0) {
console.log('Ball');
} else if (i % 3 === 0) {
console.log('Play');
} else {
console.log(i);
}
}

You can move the check for divisible by both 3 and 4 to the top:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else if (i % 3 === 0) {
console.log('Play');
} else if (i % 4 === 0) {
console.log('Ball');
} else {
console.log(i);
}
}

3 and 4 display
Above statement means, when BOTH condition satisfies then only you need to perform some task. In this scenario you need to use &&
if ( (i % 3 === 0) && (i % 4 === 0) ) {
console.log('Play Ball');
}
When to use else and/or else if
Let's take an example, you have a number, which is not divisible by 3 and 4 and you are not concern about that number. Then, you simply use else to fall all those category into it.
Now, there is another scenario, when you have entered number, which is again not divisible by 3 and 4. But, you might want see, if it is only divisible by 3 or 4. Then, you use else if where you can put the condition to check.
if ( (i % 3 === 0) && (i % 4 === 0) ) {
console.log('Play Ball');
}
else if ( i % 3 === 0) {
console.log('Play');
}
else if ( i % 4 === 0) {
console.log('Ball');
}

Related

Why is my isNaN not working in my if statement

new to JS. In the following code, the isNaN isn't working. If you enter a number in the prompt, the FizzBuzz rules work fine. However, if you enter a random string, I expect the isNaN condition to be met. What am I doing wrong?
let number = parseInt(prompt("Enter a number"));
for(let i = 1; i < number; i++) {
if(isNaN(number)) {
console.log("Is not a number")
} else 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)
}
}
When parseInt can't parse the string to an integer if will return NaN. In your for loop you use number in the condition i < number. If NaN is compared to a number using a comparison operator such as < or > the result will always be false and the loop will not run at all, this is why nothing happens when you enter a random string.
To get the result you are expecting You could put the isNaN check outside the for loop and only run the loop if number is not equal to NaN.
let number = parseInt(prompt("Enter a number"));
if (isNaN(number)) {
console.log("Is not a number")
} else {
for(let i = 1; i < number; 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)
}
}
}
As #RobinZigmond correctly pointed out, when you enter an arbitrary string, number will be NaN.
For loop basically consists of:
for (Initialization, Condition, Increment)
Your condition: let i = 1 is run one time, then your condition: i < number is checked, in this case 1 < NaN which is clearly false, as the condition here is not met, you never enter your for loop.
One way to do what you're trying to do is:
let number;
while (!isNaN(number)) {
number = parseInt(prompt("Enter a number"));
}
for(let i = 1; i < number; 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)
}
}
let userInput = prompt("Enter a number");
if(isNaN(userInput)) {
console.log("Is not a number")
} else {
let number = parseInt(userInput);
for(let i = 1; i < number; 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)
}
}
}
This works as expected (putting the isNaN check outside of the loop.
Move your first if statement out of the loop. It never gets read, because the expression parseInt(string) < number will always return false.
let number = parseInt(prompt("Enter a number"));
if (isNaN(number)) {
console.log("Is not a number")
}
for (let i = 1; i < number; 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)
}
}

FizzBuzz only prints numbers divisible by three

I am learning JavaScript, and an exercise that I am doing...I don't seem to "get" it.
The objective to write a program using console.log that prints all numbers from 1 to 100, with exceptions.
The program should print "FizzBuzz" if the number is divisible by 3 and 5.
The program should print "Fizz" only if the number is divisible by 3.
The program should print "Buzz" only if the number is divisible by 5.
If these exceptions do not apply to the numbers from 1 to 100, the number on its own should be printed.
Here is my code:
for (i = 0; i <= 100; i++) {
if (i % 3 == 0)
if (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)
}
}
Of course, this code does not work.
The numbers that do not apply to the exceptions do not print. Numbers from 1 to 100 do not print.
Any help explaining why...I would be very thankful.
Thank you.
Your attempt doesn't work, since it only logs those values to the console that are i%3 === 0, since the first if has to be true before the second block is entered.
You can see this if you log the numbers that get printed:
for (i = 0; i <= 100; i++) {
if (i % 3 == 0) // only if this returns "true" the next block will execute
if (i % 5 == 0) {
console.log("FizzBuzz " + i)
} else if (i % 3 == 0) {
console.log("Fizz " + i)
} else if (i % 5 == 0) {
console.log("Buzz " + i)
} else {
console.log(i)
}
}
Combine the first two if statements and it works!
for (i = 0; i <= 100; 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)
}
}
You need to combine the first two if statements with && (a boolean operator that means "and". For it to become true, both of the statements must be true. If one of them is false, it becomes false).
JSFiddle (open the console to see it working): http://jsfiddle.net/7236jnx4/
You can not just have this code:
if(i%3==0)
if(i%5==0){
console.log("FizzBuzz");
}
Only numbers that are divisible by 3 will be checked by the other if statements. The first two if statements need to be combined together for it to work:
if(i%5==0&&i%3==0){
console.log("FizzBuzz");
}
for (let i = 0; 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")
} else {
console.log(i);
}
}

Exercise on javascript

please I'm stuck in this question below since yesterday. Below is the question:
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).
I only got the first two conditions but not the the third. I don't know how to go about it anymore, I've tried many options. Below is my code:
<html>
<head/head>
<body>
<script type="text/javascript">
for (i = 1; i <= 100; i++)
if (i % 3 == 0) {
document.write("Fizz");
document.write("<br />");
} else if (i % 5 == 0 && i % 3 != 0) {
document.write("Buzz");
document.write("<br />");
} else if (i % 3 && 5 == 0 && i % 3 != 0 && i % 5 != 0) {
document.write("FizzBuzz");
document.write("<br />");
} else {
document.write(+i);
document.write("<br />");
}
</script>
</body>
</html>
Check the most specific (FizzBuzz) condition first.
function fizzBuzz() {
for(var 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');
} else {
console.log(i);
}
}
}
here is an updated version of your code, I keep it as you write it with some changes, I made it work without touch it's logic, you can see that the problem was in the first comparison and in the second "if else" (5 will never be equal to 0). you can optimize the code more than that, good luck.
<html>
<head/head>
<body>
<script type="text/javascript">
for (i = 1; i <= 100; i++)
if (i % 3 == 0 && i % 5 != 0) {
document.write("Fizz");
document.write("<br />");
} else if (i % 5 == 0 && i % 3 != 0) {
document.write("Buzz");
document.write("<br />");
} else if (i % 3 == 0 && i % 5 == 0) {
document.write("FizzBuzz");
document.write("<br />");
} else {
document.write(+i);
document.write("<br />");
}
</script>
</body>
</html>
Since everyone is contributing, I might as well give you an interesting solution:
var i = 101;
while(i --> 0){ // as i goes to 0... wat
var state = !!(i % 3) << 1 | !!(i % 5), // compute state?
output = ["FizzBuzz", "Fizz", "Buzz", i]; // hmm...
console.log(output[state]); // output correct string
}
1st - Instead of document.write use console.log like the question says
2nd - You have a syntax error in the head section. it should be <head></head>
3rd - for the 1st part of the question all you need is this:
for (i = 1; i <= 100; i++) {
// if i is divisible by 3
if (i % 3 == 0) {
console.log("Fizz");
}
// if i is divisible by 5 (no need to check for 3 again)
else if (i % 5 == 0) {
console.log("Buzz");
}
// else
else {
console.log(i);
}
}
4th - For the 2nd part you need to add an extra if on top of what you have already:
for (i = 1; i <= 100; i++) {
// if i is divisible by 3 and 5
if (i % 3 == 0 && i % 5 == 0) {
console.log("FizzBuzz");
}
// if i is divisible by 3
else if (i % 3 == 0) {
console.log("Fizz");
}
// if i is divisible by 5 (no need to check for 3 again)
else if (i % 5 == 0) {
console.log("Buzz");
}
// else
else {
console.log(i);
}
}
working fiddle: https://jsfiddle.net/tedmanowar/amapqcLL/
You can use a while loop, than use a nested if statements to check the conditions.
let number = 0;
while (number <= 100) {
if(number % 3 === 0 && number % 5 === 0){
console.log("FizzBuzz");
}else if(number % 3 === 0){
console.log("Fizz");
}else if(number % 5 === 0){
console.log("Buzz");
}else{
console.log(number);
}
number++;
}
This solution is the easiest and simplest. There are multiple ways to solve the question though.
for (n=1; n<=100; n++){
let output = "";
if(n % 3=== 0) output += "Fizz"
if(n % 5=== 0) output += "buzz"
console.log(output || n);
}
I don't recommend this answer - since it is very hard to maintain - but it does do it in very few lines. It also relies on the two numbers only having a common factor of 1.
for(let i=1; i<=100; i++) {
console.log(`${i%15?i%5?i%3?i:'Fizz':'Buzz':'FizzBuzz'}`)
}
This is using the ternary operator and backquote template strings.
You should just use a loop that starts at 1 and is less than 101 (so up to 100) and test for the %n === 0. In other words, make sure there is no remainder.
function startConsoleDemo(){
for(var i=1,r; i<101; i++){ // loop from 1 to 100
r = i; // default value of r
if(i % 3 === 0 && i % 5 === 0){ // if i/3 and 1/5 do not produce a remainder
r = 'FizzBuzz'; // reassign r
}
else if(i % 3 === 0){ // we knew i % 5 !== 0 so see if i/3 does not produce a remainder
r = 'Fizz'; // reassign r
}
else if(i % 5 === 0){ // we already knew i % 3 !== 0 - you know the drill
r = 'Buzz'; // reassign r
}
console.log(r); // console at each step of the loop no matter what
}
}
startConsoleDemo(); // without () you can use like a var then () later
I have a solution and I'm pretty sure it'll work for you.
for (let i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
document.write(`${i} FizzBuzz`);
} else if (i % 3 === 0 && i % 5 !== 0) {
document.write(`${i} Fizz`);
} else if (i % 5 === 0 && i % 3 !== 0) {
document.write(`${i} Buzz`);
} else {
document.write(`${i}`);
}
}

How do I find the value divisible by 4 AND 5?

I need to print "foo bar" if the value of a number between 1 and 100 is divisible by 4 and 5.
edit: both 4 and 5
Here is what I have so far:
for(i = 1; i < 100; i++){
if(i % 4 === 0){
console.log("foo");
}
else if(i % 5 === 0){
console.log("bar");
}
}
Try this:
for(i=1; i<100; i++){
if(i % 4 === 0 && i % 5 === 0) {
console.log("foobar");
}
else if(i % 4 === 0) {
console.log("foo");
}
else if(i % 5 === 0) {
console.log("bar");
}
}
Its simple maths, you can try to check it like this:
for(i=1; i<100; i++){
if(i % 20 === 0){
console.log("foo");
}
}
You can combine these statements into one if:
if(i % 4 === 0 && i % 5 === 0)
{
console.log("This number is both divisible by 4 and 5");
}
You can learn more about if and comparison operands here.
What you have is printing foo for all numbers divisible by 4 and bar for all numbers divisible by 5. Then, your code excludes 100 from the values.
The right solution to your problem is:
for(i=1; i<=100; i++){
if(i % 4 === 0 && i % 5 === 0){
console.log("foo bar");
}
}
You are actually correct. If you want to know that value is divisble by 4 and 5 you need make modulo condition on both and check whether modulo is equal to 0
so proper condition is
for ( i = 1; i < 100; i++) {
if (i % 4 == 0 && i % 5 == 0) {
console.log("number is divisble by 4 and 5");
}
}
Just try this:
for(var i = 1; i < 100; i++){
var result;
if(i % 4 === 0){
result = "foo";
}
//... perhaps, there are any conditions
if(i % 5 === 0 && result){
result += "bar";
console.log(result);
}
}
Second way:
for(var i = 1; i < 100; i++){
var result;
if(i % 4 === 0){
result = "foo";
}
//... perhaps, there are any conditions
if(i % 5 === 0 && result){
result += "bar";
} else result = "";//or undefined
if(result) console.log(result);
}

Stuck in Codecademy's Javascript fizzbuzz app [duplicate]

This question already has answers here:
FizzBuzz program (details given) in Javascript [closed]
(27 answers)
Closed 4 years ago.
Here are Codecademy's instructions:
Print out the numbers from 1 - 20.
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.
And here is my code:
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);
}
}
The problem is that it won't print "FizzBuzz" for the number 15. It just prints "Fizz".
What am I missing here?
The else if only runs if all of the other statements so far have been false. Since i % 3 is true, the remaing else/if statements will never run. Try something like this:
for (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("Fizz");
}
else {
console.log(i);
}
}
If you want to use a nested conditional this seems to work.
for ( i=1 ; i < 21 ; i++){
if(i % 3 === 0) {
if(i % 5 === 0){
console.log("FizzBuzz");
}
else {
console.log("Fizz");
}
}
else if (i % 5 === 0) {
if ( i % 3 === 0){
console.log("FizzBuzz");
}
else{
console.log("Buzz");
}
}
else{
console.log(i);
}
}
straight forward fizzBuzz in javascript using tenary operators(a one-line shorthand for an if-else statement)
to learn about tenary operators go here
var i = 1;
while (i <= 20){
console.log((i % 3 === 0 && i % 5 === 0) ? "FizzBuzz" : (i % 3 === 0) ? "Fizz" : (i % 5 === 0 ? "Buzz" : i));
i++;
}

Categories