0

When i press 1 to go to "Start" it runs start BUT it also runs "instructions". So after it says "Welcoe22222" it would say "HAHAHAH" right after it but i only want the "start" function.

<script type="text/javascript">


alert("Welcome to the BattleShip Game!!!")
name = prompt("What is your name: ")
alert("Welcome "+name)
option = prompt("1. Start Game \n \n 2. Instructions \n \n Select: ")
if (option ==1)
    start()
else (option ==2)
    instructions()

function start() {

    alert("Welcoe22222")

}

function instructions() {

    alert("HAHAHAH")

}

</script>
4
  • 3
    This is why you should use semicolons, at least until you fully understand JS syntax. Commented Dec 21, 2016 at 20:25
  • @SLaks I hate semicolons, but perhaps you mean brackets? Commented Dec 21, 2016 at 20:26
  • 1
    @Saiid: No; I mean semicolons (although braces would also help). You can't rely on ASI until you fully understand how JS syntax works. Commented Dec 21, 2016 at 20:27
  • The semicolons aren't the problem, though. If they had used the brackets in the if/else, it wouldn't be doing the same thing. Commented Dec 21, 2016 at 20:29

3 Answers 3

4

Your else syntax is completely wrong.

If you want else if, you need to write that.

Your code is parsed using automatic semicolon insertion as

if (option ==1)
    start()
else
    (option ==2);
instructions()
Sign up to request clarification or add additional context in comments.

Comments

0

You are missing an if

else if (option == 2)
//   ^^

Better to use block statement { /* ... */ }, like

if (option == 1) {
    start();
} else if (option == 2) {
    instructions();
}

Comments

0

First I think for not getting confusing, or making your teammates confused about your code, it is better to use block statement (I mean if you think in the future you may want to add more lines of codes in the if statement.

You can't have parentheses after else. if you sayelse (x==y)`, you will get the error:

Uncaught SyntaxError: Unexpected token {


It should be:

if (option == 1) {
    start();
} else if (option == 2) {
    instructions()
}

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.