0

I am using this code as a part of a game I'm making. But for the purposes of finding out what was causing this bug I took the section of code into a new html document. I am trying to use this code:

<html>
<script type="text/javascript">
Difficulty = Normal
function ChangeGameMode()
{
  alert (Difficulty + ' game started ');
  }
</script>
<button type="button" class="StartButton" onclick="ChangeGameMode()">Start</button>
</html>

When I click the button, I don't get an alert. Nothing happens.

But if I don't use the Difficulty variable in the alert - ie:

alert ('Normal' + ' game started ');

Then when I use that for the alert instead, it does work. I am at conplete loss as to what is causing this and I have been pulling my hair out because of it. Any help would be appreciated.

3
  • What is the Normal variable? Did you want a string? Commented Oct 27, 2012 at 15:09
  • Btw, you should lowercase all function and variable names unless they're constructors Commented Oct 27, 2012 at 15:09
  • Always check the Javascript console for error messages when something "does not work" Commented Oct 27, 2012 at 15:11

2 Answers 2

4

You haven't properly defined the Difficulty variable. You need to assign it a valid value. Normal is not a valid javascript value. You need to wrap it in quotes:

<html>
<script type="text/javascript">
var Difficulty = 'Normal';
function ChangeGameMode() {
    alert (Difficulty + ' game started ');
}
</script>
<button type="button" class="StartButton" onclick="ChangeGameMode()">Start</button>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Derp. I forgot to use normal as a string. I'm so used to using numbers for my variables instead of words.
0

Normal must be in quotes:

var Difficulty = "Normal";

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.