5

I wrote a simple JavaScript code that allows people to enter a number and it should return either FizzBuzz, Fizz, Buzz or just the number if it did not meet the conditions of the FizzBuzz game. I then tried to create a button in HTML with the following code:

<button onclick="fBCalc(prompt("Enter a number!"))">FizzBuzz</button>

I have also tried this:

<input type="button" name="FizzBuzz" value="FizzBuzz" onClick="fBCalc(prompt("Enter a number!"))" />

Both methods did not work. I have searched Google for an answer but I have tried them and nothing happens when I click the button. I made sure that the function was called correctly. This works without the button so I do not know why it shouldn't work with a button. Unless a button is unable to prompt o.o. Does anyone know what is wrong?

Here is a pastebin to my full HTML code: http://pastebin.com/YPGdbTVQ

1
  • 1
    I'm sensing a disturbance in your copy-paste. Try again? Commented Aug 15, 2012 at 3:52

2 Answers 2

6

You haven't pasted your code properly here, but I did spy what I suspect is your issue on your Pastebin:

<button onclick="fBCalc(prompt("Enter a number!"))">FizzBuzz</button>

You can't put quotes inside of quotes the way you have here, because the browser can't figure out which ones do what. Try swapping the inner quotes with single-quotes:

 <button onclick="fBCalc(prompt('Enter a number!'))">FizzBuzz</button>

Coincidentally, good luck with your grades! ;)

Sign up to request clarification or add additional context in comments.

7 Comments

Wow! That was it. I totally forgot that I couldn't use the double quotation marks inside another double quotation mark. I will keep that in mind for the future :).
No worries. I'm a professional (allegedly) and I catch myself doing that a depressing amount of the time. All I can suggest is make sure you use an editor with text highlighting - it'll catch stuff like this by messing up the highlight.
Yeah, I use Notepad++ and thought the highlighting was a bit weird.
Gotcha. Yeah, it takes a while to be able to intuit what's actually wrong - especially early on, EVERYTHING makes your brain go "meeeeaaaaaghiwannawatchTV."
Yeah.. I am kind of struggling with objects and methods atm :(. Would you happen to know how I can easily learn this?
|
0

you cant put double quotes inside double quotes, so make your code like this

<button onclick="fBCalc(prompt('Enter a number!'))">FizzBuzz</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.