# FizzBuzz, js beginner

Hi! I’m going through FizzBuzz.

Given an integer n, return a string array answer (1-indexed) where:

``````answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
answer[i] == "Fizz" if i is divisible by 3.
answer[i] == "Buzz" if i is divisible by 5.
answer[i] == i (as a string) if none of the above conditions are true.
``````

Here’s my wrong but honest code:

``````let i = 1;

let fizzBuzz = function(n) {
for (i <= n, 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 { // handles numbers not divisible by 3 or 5
console.log(i);
}
};
``````

How can I improve my code to make it right?

``````let n:Number = prompt("You give me number, I give you FizzBuzz");
let fizzBuzz = function(n) {
for (i <= n, 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 { // handles numbers not divisible by 3 or 5
console.log(i);
}
``````
``````let n:Number = prompt("You give me number, I give you FizzBuzz");
let fizzBuzz = function(n) {
for (i <= n, 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 {
// handles numbers not divisible by 3 or 5
console.log(i);
}
``````

It helps if you indent in a common fashion.

You don’t have enough }

You should review how for loops work.

As was mentioned earlier, you have a few syntax errors that are preventing you from running your code properly.

Aside from that, there are a few other things to consider.

Since you are dealing with getting user inputs, you will need practice how to handle different input situations.

For example, what happens if the user doesn’t input anything when prompted?

You could add a condition in your fizzBuzz function that checks for empty inputs and shows an alert to the user to provide a number

Or you can add a do…while loop to continually prompt the user to comply into giving a number

The other thing to consider if a user inputs something that is clearly not a number like this:

`"alskdjflkj"`

I would strongly suggest casting the user input to a number here

``````let n = Number(prompt("You give me number, I give you FizzBuzz"));
``````

Then you can check if the input is not a number and show an alert that the user needs to provide a valid number

Lastly, you shouldn’t get into the habit of using one letter variables names

``````let n
``````

When it comes to naming your variables you will want to use something more meaningful to the value like `userNumber`

Hope that helps!

2 Likes
``````let fizzBuzz = (n) => {
for (let i = 1; i <= n; i++) {
if (i % 3 === 0 && i % 5 == 0) {
} else if
(i % 3 === 0) {
} else if
(i % 5 === 0) {
} else {
// handles nums !divisible by both
}
}
}
``````

Do you have further questions?

It looks right to me. Nice work!

Here is standard indentation applied to your code:

``````const fizzBuzz = (n) => {
for (let i = 1; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
} else if (i % 3 === 0) {
} else if (i % 5 === 0) {