Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 10, 2017 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codecademydev/d99962a8f3817fc5b9606b6f8d2dfc80 to your computer and use it in GitHub Desktop.
Save codecademydev/d99962a8f3817fc5b9606b6f8d2dfc80 to your computer and use it in GitHub Desktop.
Codecademy export
//4 outcomes are:
var o1 = 'JavaScript wins!';
var o2 = 'You win!';
var o3 = 'A Draw';
var pcChoice = 0;
var userChoice = 0;
var pcShows = 'none';
var userShows = 'none';
var outcome = 'none';
console.log('Rock Paper Scissors Game in JavaScript');
while (userChoice < 1 || userChoice > 3 || isNaN(userChoice)) {
userChoice = prompt('1 = Rock, 2 = Papper, 3 = Scissors'); userChoice = parseInt(userChoice);}
if (userChoice === 1){userShows = 'a Rock';}
if (userChoice === 2){userShows = 'Paper';}
if (userChoice === 3){userShows = 'Scissors';}
while (pcChoice < 1 || pcChoice > 3) {
pcChoice = Math.floor(Math.random() * 4);}
if (pcChoice === 1){pcShows = 'a Rock';}
if (pcChoice === 2){pcShows = 'Paper';}
if (pcChoice === 3){pcShows = 'Scissors';}
//describe the result
console.log('PC throws ' + pcShows + ' as you throw ' + userShows + '!');
//decide winner or draw
if ((userChoice === 1) && (pcChoice === 1)){outcome = o3;}
if ((userChoice === 1) && (pcChoice === 2)){outcome = o1;}
if ((userChoice === 1) && (pcChoice === 3)){outcome = o2;}
console.log(' ');
console.log(outcome);
@eatAsnack
Copy link

eatAsnack commented Mar 10, 2017

if someVar has a string value like 'Dave' and you call parseInt(someVar) and if next you call isNaN(someVar) It still returns true. So it looks like parseInt() will not convert all strings to ints and will only do so in cases where the string value, e.g. '2' would be an integer by removing its single quotes. I will check what parseInt() does to a float type for 0.47 and val 0.00, I suspect they become NaN but I don't know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment