Javascript For Loops for rules - javascript

I am new to Javascript and am having trouble with the following code.
for (var i = 1; i < 21; i++)
{
console.log (i);
}
if (i % 3) {
console.log("Fizz");
}
else if (i % 5) {
console.log("Buzz");
}
else (i % 3 && 5) {
console.log("FizzBuzz");
}
The instructions are:
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.

for (var i = 1; i < 21; i++) {
if (!(num % 3) && !(num % 5)) {
console.log('FizzBuzz');
} else if (!(num % 3)) {
console.log('Fizz');
} else if (!(num % 5)) {
console.log('Buzz');
} else {
console.log(i);
}
}

Move your conditional code inside the loop and use if instead of else-if for your last condition will be completed in first two if:
for (var i = 1; i < 21; i++)
{
console.log (i);
// make it readable
if (i % 3 == 0) {
console.log("Fizz");
}
if (i % 5 == 0) {
console.log("Buzz");
}
}

if (!(num % 3) && !(num % 5)) {
alert('FizzBuzz');
} else if (!(num % 3)) {
alert('Fizz');
} else if (!(num % 5)) {
alert('Buzz');
}

for (var i = 1; i < 21; 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");
}
}

<html>
<head>
</head>
<body>
<script>
for(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); }
};
</script>
</body>
</html>

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

Fizz Buzz question with extra difficulty layer

I am trying to solve the Fizz Buzz question with an extra layer.
This is what I have so far. All this works fine but I need one more extra condition.
It's JS Unit testing
For any other input (valid or otherwise) return a string representation of the input
printFizzBuzz = function(input) {
// Your code goes here
for (var i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
};
Thank you!
You can use .toString() (JS toString) to change the number in string:
function printFizzBuzz(input) {
// Your code goes here
for (var i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i.toString());
}
}
};
printFizzBuzz();
Looking at the question, it is not asking for a loop, it is asking for any value passed to the function. So the answer should look more like this
function printFizzBuzz(input) {
// Your code goes here
if (typeof input !== ‘number’) {
return String(input)
}
let by3 = input % 3
let by5 = input % 5
switch(0) {
case by3 + by5:
return "FizzBuzz"
case by3:
return "Fizz"
case by5:
return "Buzz"
default:
return input.toString()
}
}
}
If I was the interviewer, I would prefer the answer to look more like this
function printFizzBuzz(input) {
// Your code goes here
for (var i = 1; i <= 20; i++) {
let by3 = i % 3
let by5 = i % 5
switch(0) {
case by3 + by5:
console.log("FizzBuzz")
break
case by3:
console.log("Fizz")
break
case by5:
console.log("Buzz")
break
default:
console.log(i.toString())
}
}
}
}
printFizzBuzz()

Else/If Loop 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');
}

This fizzbuzz code isn't working in javascript

Hi I'm pretty new to coding. This fizzbuzz code isn't working any help would be greatly appreciated. I'm using sublime text 3 and trying to produce results on vagrant using the node command. Thank You - Saad
for(i=101; i<201; i++) {
if (i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");
};
if(i % 3 === 0) {
console.log("Fizz");
} else if(i % 5 === 0) {
console.log("Buzz")
} else {
console.log(i);
}``
}
This could be done like this:
for(i = 101; i < 201; i++) {
var r = ""
if (i % 3 === 0){
r += "Fizz";
}
if(i % 5 === 0) {
r += "Buzz";
}
if(i % (5 * 3) !== 0){ // or just 15
r = i;
}
console.log(r);
}

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