Reputation: 1
I am fairly new to JavaScript and made a VERY basic choose your own adventure game with a switch statement. When I attempt to call my function using a button it is unsuccessful. My JavaScript alone is this.
function chooseGame(){
var troll = prompt("You are walking through a forest on your way to GameStop. You come across a troll guarding the only bridge to get there, he says if you pay him he will let you by. Do you PAY, KILL, or CHALLENGE HIM TO A GAME OF CHESS?").toUpperCase();
switch(troll){
case'PAY':
var money = prompt("Do you have money").toUpperCase();
var amount = prompt("IS it $50?").toUpperCase();
if (money && amount === "YES"){
console.log("You pay him and steal the game you wanted, get caught, and go to prison.");
}else{
console.log("He broke your legs and stole your shoes, not like you'd be needing them.");
}
break;
case'KILL':
var weapon = prompt("Do you have a weapon?").toUpperCase();
var friend = prompt("Do you have friends with you?").toUpperCase();
if (weapon || friend === "YES"){
console.log("You break his legs and take his money buying multiple games.");
}else{
console.log("He brings you back to his personal torture chamber and laughs at your pain for all eternity.");
}
break;
case'CHALLENGE HIM TO A GAME OF CHESS':
var clever = prompt("Are you clever?").toUpperCase();
var ability = prompt("Are you good at chess?").toUpperCase();
if (clever || ability === "YES"){
console.log("You beat him and go on your way to GameStop.");
}else{
console.log("You aren't clever or good at chess, then why did you challenge a troll to a game?");
}
break;
};
}
With my HTML it looks like this.
<html>
<head>
<Script language = "Javacript">
function chooseGame(){
var troll = prompt("You are walking through a forest on your way to GameStop. You come across a troll guarding the only bridge to get there, he says if you pay him he will let you by. Do you PAY, KILL, or CHALLENGE HIM TO A GAME OF CHESS?").toUpperCase();
switch(troll){
case'PAY':
var money = prompt("Do you have money").toUpperCase();
var amount = prompt("IS it $50?").toUpperCase();
if (money && amount === "YES"){
console.log("You pay him and steal the game you wanted, get caught, and go to prison.");
}else{
console.log("He broke your legs and stole your shoes, not like you'd be needing them.");
}
break;
case'KILL':
var weapon = prompt("Do you have a weapon?").toUpperCase();
var friend = prompt("Do you have friends with you?").toUpperCase();
if (weapon || friend === "YES"){
console.log("You break his legs and take his money buying multiple games.");
}else{
console.log("He brings you back to his personal torture chamber and laughs at your pain for all eternity.");
}
break;
case'CHALLENGE HIM TO A GAME OF CHESS':
var clever = prompt("Are you clever?").toUpperCase();
var ability = prompt("Are you good at chess?").toUpperCase();
if (clever || ability === "YES"){
console.log("You beat him and go on your way to GameStop.");
}else{
console.log("You aren't clever or good at chess, then why did you challenge a troll to a game?");
}
break;
};
}
</Script>
</head>
<body>
<button type="button" onclick = "myFunction();">Game Start</button>
</body>
</html>
When I run my code, as a .html, it displays the button but doesn't do anything when I click it. I was able to make the button alert so I believe this is a problem with my code not calling my function, can someone please alert me if I am doing anything wrong.
Upvotes: 0
Views: 327
Reputation: 1
if (weapon || friend === "YES")
you can't write like that.
if (weapon === "YES" || friend === "YES")
Upvotes: 0
Reputation: 10058
There is no function called myFunction(). You want chooseGame()
<button type="button" onclick="chooseGame();">Game Start</button>
(also removed the spacing around the equal sign. that probably isn't breaking it, but is not legal HTML).
Upvotes: 2