From ab4bc64d65d41988e38cfeffda0805b2e9140a38 Mon Sep 17 00:00:00 2001 From: LearningNerd Date: Sat, 21 Mar 2015 13:17:36 -0700 Subject: [PATCH] first commit --- Battleship-JavaScript/Battleship.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Battleship-JavaScript/Battleship.js diff --git a/Battleship-JavaScript/Battleship.js b/Battleship-JavaScript/Battleship.js new file mode 100644 index 0000000..7ce2127 --- /dev/null +++ b/Battleship-JavaScript/Battleship.js @@ -0,0 +1,18 @@ +// this function generates a random number between a specified minimum and maximum number +function getRandomNumber(min, max) { + return Math.floor(Math.random() * (max - min) + min); +} + +// for our game, we'll pick a number from 1 to 5. This is where our "battleship" is. +var correctAnswer = getRandomNumber(1,5); + +//start the game: +var userInput = parseInt(prompt("Let's play Battleship! To fire at a battleship, please enter another number from 1 to 5:"), 10); + +// the game loop: keep asking the player to guess until they guess the right answer +while (userInput !== correctAnswer) { + var userInput = parseInt(prompt("You missed! To fire again, please enter another number from 1 to 5:"), 10); +} + +// the code below will only run when the game loop ends: +alert("You win! Battleship sunk!") \ No newline at end of file