Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

rock paper scissors js

/*
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 :: hypot javascript 
Javascript :: mdn clonenode 
Javascript :: tsconfig build only files and not src 
Javascript :: how to write a program that shows a random number between 1 and 100 in your browser 
Javascript :: comments js 
Javascript :: react extends component construtor super props 
Javascript :: react useid hook 
Javascript :: json to formdata 
Javascript :: array json 
Javascript :: NODEJS ES6 STRING TO BASE64 
Javascript :: change img src css 
Javascript :: react using proptypes 
Javascript :: js contain character 
Javascript :: create document mongoose 
Javascript :: npm rebuild node-sass 
Javascript :: java object to json 
Javascript :: json full form 
Javascript :: Disabling right click using Javascript 
Javascript :: watch file in changes in webpack 
Javascript :: javascript date for 5 seconds from now 
Javascript :: javascript require 
Javascript :: make a file downloadable in react 
Javascript :: fivem esx script 
Javascript :: datatable index column server side 
Javascript :: javascript array to string with commas 
Javascript :: bson to json converter 
Javascript :: Sort an array to have specific items 
Javascript :: react arrow funvtion 
Javascript :: faker js 
Javascript :: javascript return string 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =