Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript rock paper scissors

/*
Options
(Note: each option beats the next option in the array
although the last option in the array beats the first.
This means you can add more options if you wish.)
*/
const options = [ "rock", "scissors", "paper" ];

// do while ensures the body is executed at least once
do {
	// get user input
	let input = window.prompt(options.map((option, idx) => "enter " + idx + " for " + option).join(", "));
	// check if the user pressed "Ok" or "Cancel"
	if (typeof input === "string") {
		// remove all non-numeric characters from input
		input = input.replace(/[^0-9]/g, "");
		// if the user has input a valid number
		if (options[input]) {
			// print the user's choice
			window.alert("You picked " + options[input] + "!");
			// generate the computer's choice
			const computer_choice = ~~(Math.random() * options.length);
			// print the computer's choice
			window.alert("The computer picked " + options[computer_choice] + "!");
			// process choices
			let you_beat = input + 1, computer_beats = computer_choice + 1;
			if (you_beat > options.length) {
				you_beat = 0;
			}
			if (computer_beats > options.length) {
				computer_beats = 0;
			}
			// print outcome
			if (you_beat === computer_choice) {
				window.alert("You win! :D");
			} else if (computer_beats === input) {
				window.alert("You loose... :(");
			} else {
				window.alert("It was a tie! :|");
			}
        } else {
			window.alert("Oh no! You didn't enter a number...");
		}
	} else {
		window.alert("Woops! You didn't enter anything or your input was not in the correct format...");
	}
} while (window.confirm("Play again?"));
Comment

PREVIOUS NEXT
Code Example
Javascript :: sort method in js 
Javascript :: angular remove index of array 
Javascript :: access variable from another function javascript 
Javascript :: drupal8 get params from route 
Javascript :: how to use js console log 
Javascript :: jquery onclick anchor tag scroll to div with exact position 
Javascript :: require mongoose 
Javascript :: How to abreviate digits in js 
Javascript :: combine 2 arrays javascript 
Javascript :: ReactJS Axios Delete Request Code Example 
Javascript :: Scroll elementleft using js 
Javascript :: state hook is not updating react 
Javascript :: how to connect mysql using node js stack 
Javascript :: material ui dark theme 
Javascript :: javascript The toString() Method 
Javascript :: vue add external script 
Javascript :: how to see if checkbox is checked 
Javascript :: print page using js 
Javascript :: jquery append to table 
Javascript :: ajax actions wordpress 
Javascript :: onsubmit in js 
Javascript :: fullcalendar react add event duration 
Javascript :: axios request and response intercepters 
Javascript :: if window width jquery then display a div at scroll 
Javascript :: javaScript getHours() Method 
Javascript :: split string into two parts javascript 
Javascript :: react-bootstrap sidebar 
Javascript :: angular how to check a radiobox 
Javascript :: object destructuring 
Javascript :: nuxt add plugin 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =