4

How do I change the output depending on whether or not it is divisible by 3 or 5? If it is divisible by 3, I want to show "rock" and if it's divisible by 5 I want to show "star" (similar to in FizzBuzz). If both, they'll see both.

Here's my code:

if (var n = Math.floor((Math.random() * 1000) + 1); {
  var output = "";
  if (n % 3 == 0)
    output += "Rock";
  if (n % 5 == 0)
    output += "star";
  prompt(output || n);
}

Why isn't my code working properly?

Corey Blinks
  • 57
  • 1
  • 1
  • 4
  • What are the symptoms or errors? – winwaed Jun 29 '15 at 01:48
  • Uncaught SyntaxError: Unexpected token var at Object.InjectedScript._evaluateOn (:895:140) at Object.InjectedScript._evaluateAndWrap (:828:34) at Object.InjectedScript.evaluate (:694:21) – Corey Blinks Jun 29 '15 at 01:52
  • What I'm trying to do is the create some more code around the Math.Floor(Math.random()*1000 + 1); which will test whether the number is divisible by 3 or 5 and depending on the result. – Corey Blinks Jun 29 '15 at 01:53
  • @Claies - No, the `;` is not a syntax error, try it and see. It merely makes the code unlikely to do what is intended (it terminates the `if` statement). The `var` inside the `if` is the syntax error. – Michael Geary Jun 29 '15 at 02:46
  • 1
    @CoreyBlinks - When you get a syntax error and you don't know what is causing it and the error message does not point you to the line number, one way to troubleshoot is to start taking code out until the syntax error goes away. In this code, you can take out *everything* after the `if` statement and the syntax error will still be there. So now you will know which line of code contains the syntax error. From there you can start studying some JavaScript reference material to learn what is and isn't allowed inside the parentheses of an `if` statement. – Michael Geary Jun 29 '15 at 02:49

5 Answers5

4

var n = Math.floor((Math.random() * 1000) + 1);
if (n) {
  var output = "";
  if (n % 3 == 0)
    output += "Rock";
  if (n % 5 == 0)
    output += "star";
  prompt(output || n);
}

The var inside the if statement is a syntax error. My browser shows this error:

SyntaxError: expected expression, got keyword 'var'

So I think you should declare variable n before telling the if statement that var n is your comparison expression.

Burning Crystals
  • 1,157
  • 3
  • 19
  • 35
  • Yes the n is going out of scope due to the ; – winwaed Jun 29 '15 at 01:56
  • 1
    Thanks! I just realized it made no sense to have the prompt command. It makes better sense to have console.log to show them their result. – Corey Blinks Jun 29 '15 at 01:57
  • ^+1, console.log() is a good choice in debugging since you only have to refresh the page and get results without obtrusion in the dom. :) – Burning Crystals Jun 29 '15 at 01:58
  • 2
    @winwaed - No, `n` is not going out of scope because of the `;`. JavaScript does not have block scope. – Michael Geary Jun 29 '15 at 02:43
  • 2
    @BurningCrystals - "It's not a bad idea to declare a variable outside the if ()." You should make that statement a little stronger: The `var` inside the `if` is a syntax error. And: "also, you had a syntax error in your `if()` statement since it had a semicolon after the `)`" No, that is not a syntax error, it is perfectly legitimate syntax, even though it doesn't do what you might want. – Michael Geary Jun 29 '15 at 02:45
  • @MichaelGeary thanks for pointing this out! I cant explain it any better as I understood it. This is much help. – Burning Crystals Jun 29 '15 at 02:48
  • Thanks for the correction @Michael. I'm not a huge fan of Javascript :-) – winwaed Jun 29 '15 at 13:02
1
let f = "fizz";
let b = "buzz";
for (let num = 1; num <=700 ; num++) {
    if (num% 3 === 0 && num % 5 ===0){
    console.log(num + f + b);
                }
    else if (num % 5 === 0){
    console.log(num+b);
                }
    else if (num % 3 === 0){
    console.log(num+f);
                }
    else {
    console.log(num);
                }
            }
0
var fizzBuzz = function(n) {
    let arr = []
    for(var x = 0;x< n;x++){
        if((x + 1) % 3 === 0 && (x + 1) % 5 !== 0){   
            arr.push("Fizz")
        } else if((x + 1) % 5 === 0 && (x + 1) % 3 !== 0){
            arr.push("Buzz")
        } else if((x + 1) % 3 === 0 && (x + 1) % 5 === 0 ){
             arr.push("FizzBuzz")
        } else{
             arr.push(String(x + 1))
        }
    }
    return arr
};
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 25 '22 at 08:20
0
function isDivisible(num, divisor){
  return !(num%divisor)?true:false;
}
isDivisible(3, 15); // false
isDivisible(15,3); // true
isDivisible(15,5); // true
Toby Harnish
  • 90
  • 1
  • 9
0
        int []num=new int[]{3,5,15,4};
        if(num.Length==0)
        { 
            Console.Write("No Number Entered");
        }
        foreach(int i in num)
        {
            if(i%3==0 && i%5==0)
                Console.WriteLine("rock star");
            else if(i%5==0)
                Console.WriteLine("star");
            else if(i%3==0)
                Console.WriteLine("rock");
            else
                Console.WriteLine("The number is not divisible by 3 and 5 :{0}",i);
        }
        
    }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 17 '22 at 06:55