/    Sign up×
Community /Pin to ProfileBookmark

This works in Firefox and Chrome, but not in Safari

Hey,

First post here. This is a simple game I created within my HTML5 doc. It’s “Rock, Paper, Scissors.” Can someone tell me why this code runs as it should in FF and Chrome, but not in Safari?

The problem I’m having is that when I run the code, if the user hit’s “cancel” before any text is submitted via the prompt, an alert dialogue should show up and say “That’s cool.” Otherwise, if any text is entered, and the user does not enter either rock, paper or scissors, the dialogue says “I said rock, paper or scissors!” In FF and Chrome, if the user cancels the initial prompt to play the game, it returns “false,” the alert “That’s cool.” appears, and the loop stops. If this is run in Safari, the prompt “I said rock, paper or scissors!” appears.

So…Safari skips the “That’s cool” else/if when the initial prompt is cancelled.

Thanks.

[QUOTE]

<script type=”text/javascript”>
function runGame()
{
var result = “”;

var player = prompt(“Pick rock, paper, or scissors.”);
if (player !== null) {
player = player.toLowerCase();
}

var choices = [“rock”,”paper”,”scissors”];
var computer = choices[Math.floor(Math.random()*3)];

var win = “Your “+player+” beats “+computer+”. You win.”;
var winScissors = “Your “+player+” beat “+computer+”. You win.”;
var lose = “Your “+player+” loses to “+computer+”. You lose.”;
var loseScissors = “Your “+player+” lose to “+computer+”. You lose.”;
var draw = “A draw: “+player+” on “+computer+”.”;

if(player === “rock”){
if(computer === “scissors”){
result = win;
}
else if(computer === “paper”){
result = lose;
}
else if(computer === “rock”){
result = draw;
}
}
else if(player === “paper”){
if(computer === “rock”){
result = win;
}
else if(computer === “scissors”){
result = lose;
}
else if(computer === “paper”){
result = draw;
}
}
else if(player === “scissors”){
if(computer === “paper”){
result = winScissors;
}
else if(computer === “rock”){
result = loseScissors;
}
else if(computer === “scissors”){
result = draw;
}
}
else if (player === null) {
alert(“That’s cool.”);
return false;
}
else if (player !== “rock” || player !== “paper” || player !== “scissors”) {
result = (“I said rock, paper or scissors!”);
}
var repeat = confirm(result + “nnDo you want to play again?”);
if (repeat === false) {
alert(“Thanks for playing!”);
}
else {
runGame();
}
}
</script>

[/QUOTE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@007JulienMar 25.2012 — After a cancel, an alert(typeof(player)) shows an object with IE (whitout HTML5) and FireFox, but a string with FireFox. Replace if (player === null) by if (!Boolean(player)) which is always true with null "" or 0. Since, It would be more practical to click on images and to see statistics...
Copy linkTweet thisAlerts:
@ZICKONEZEROauthorMar 25.2012 — Thanks! That kind of worked, but if it were to run exactly the way I wanted it to, hitting "cancel" on the first prompt would alert("That's cool."), and hitting "ok" with nothing typed would alert("I said Rock, Paper or Scissors!")

Right now hitting both Cancel and OK on a blank prompt returns "That's cool." I can live with that though.

Is there any way to distinguish between hitting OK with nothing typed and hitting "Cancel?"
Copy linkTweet thisAlerts:
@ZICKONEZEROauthorMar 29.2012 — Is there any way to distinguish between hitting OK with nothing typed and hitting "Cancel?"
Copy linkTweet thisAlerts:
@mrhooMar 30.2012 — Put a single space in the prompt's 2nd parameter and look for the single space

to indicate the player pushed OK without changing it.

You'll get an empty string from Safari on cancel, and null on most others.

You'll want to strip any trailing or leading spaces from the input after you test for !player.

var player = prompt("Pick rock, paper, or scissors."," ");
Copy linkTweet thisAlerts:
@ZICKONEZEROauthorMar 31.2012 — Thanks that worked. It's kind of hack almost but hey, mission accomplished ?

Now can anyone tell me why Firefox reloads the page, bringing me back to the home page after the JavaScript game finishes?

Put a single space in the prompt's 2nd parameter and look for the single space

to indicate the player pushed OK without changing it.

You'll get an empty string from Safari on cancel, and null on most others.

You'll want to strip any trailing or leading spaces from the input after you test for !player.

var player = prompt("Pick rock, paper, or scissors."," ");[/QUOTE]
×

Success!

Help @ZICKONEZERO spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...