Skip to content

Instantly share code, notes, and snippets.

@zzyou
Last active April 14, 2018 19:42
Show Gist options
  • Save zzyou/16262a2ae2b98cc1b149ece676b1b559 to your computer and use it in GitHub Desktop.
Save zzyou/16262a2ae2b98cc1b149ece676b1b559 to your computer and use it in GitHub Desktop.
Zhenzhen's JavaScript Objectives. Rock Paper Scissor Game. April 2018.
/* Section 1 - JavaScript Objectives.
Techtonica
JS1: variables, strings, integers.
JS2: arrays, functions.
JS3: booleans.
JS4: loops.
JS5: switch statements and functions.
JS6: object literals.
JS7: regular expressions.
JS8: object oriented programming.
Rithm School
1. errors, debugging.
2. nested objects, nested arrays.
3. higher order functions, timers, closures.
I have met my learning objectives: completed the materials and finished the exercises.
I will spend more time to cover more of Rithm School's materials and exercises.
*/
/* Section 3 - React Reflection.
Before starting my React Calculator project, I finished the Learn ReactJS Part I course at Codecademy.
That course gave me a good understanding of ReactJS so that I could finish most of my project.
The challenge came when I wanted to incorporate my jQuery calculation function into React.
I didn't know where to put the function, and how to combine it smoothly with setState.
So I looked at other people's React code, and found out that I could put the function in a separate file and import it to my main JS file.
Then, to prepare the ReactJS presentation for Pantheon, I finished the Learn ReactJS Part II course at Codecademy.
I felt more comfortable. I know a good presentation or explanation comes from a really good understanding.
I want to thank the Codecademy courses and React project and Pantheon presentation, which give me a good start of ReactJS.
*/
/* Section 2 - Code.
Rock / Paper / Scissor
Build a game of Rock/Paper/Scissor where through the use of the prompt function,
a user can enter their choice
and based on a random selection - they can either tie/win or lose against a computer.
*/
function game() {
let userChoice = prompt('What is your choice of rock, paper, or scissor?').toLowerCase();
let choice = ['rock', 'paper', 'scissor'];
let shortChoice = ['r', 'p', 's'];
let random = Math.floor(Math.random() * 3);
let computerChoice = choice[random];
validation();
function validation() {
if (choice.includes(userChoice) || shortChoice.includes(userChoice)) {
gameLogic();
return endGame();
}
// If the user enters nothing, he or she may want to end the game.
else if (userChoice.length === 0) {
return endGame();
}
userChoice = prompt('Invalid input. Please only enter rock, paper or scissor.').toLowerCase();
return validation();
}
function gameLogic() {
// gameLogic() always runs with validation(),
// which means userChoice meets the criteria of choice or shortChoice,
// so only the first index of userChoice needs to be checked.
if (userChoice[0] === computerChoice[0]) {
return alert(`${userChoice} V.S. ${computerChoice}. It's a tie!`);
}
else if ( ( userChoice[0] === 'r' && computerChoice === 'paper' )
|| ( userChoice[0] === 's' && computerChoice === 'rock' )
|| ( userChoice[0] === 'p' && computerChoice === 'scissor') ) {
return alert(`${userChoice} V.S. ${computerChoice}. You lose.`);
}
return alert(`${userChoice} V.S. ${computerChoice}. You win!`);
}
function endGame() {
let end = confirm('Do you want to end the game?');
if (end) {
return alert('Thank you for playing!');
}
return game();
}
}
game();
/* How to set this game up:
1. Copy this game funtion to your browser's console.
2. Call this function, game();
3. Enjoy! */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment