Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules
node_modules
56 changes: 47 additions & 9 deletions game.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
const rls = require('readline-sync')
const rls = require("readline-sync")

/**
* Starts the game by prompting the user if they want to play
* Calls either gameLoop() or quitGame()
*
* @returns {undefined}
*/
const startGame = () => {

const startGame = () => {
if(rls.keyInYN("Do you want to start playing the game? Y or N")){
console.log("Let's start!")
gameLoop()
}else {
console.log("Have a nice life!")
quitGame()
}
}

/**
* Logs "Goodbye!"
* Calls process.exit() to quit the game
*
* @returns {undefined}
*/
const quitGame = () => {

const quitGame = () => {
console.log("Goodbye!")
process.exit()
}

/**
Expand All @@ -27,18 +35,48 @@ const quitGame = () => {
*
* @returns {undefined}
*/
const gameLoop = () => {

const gameLoop = () => {
console.log("I have a random number in mind")
console.log("It's between 1 and 1000")
console.log("You have 10 guesses total")
let guessCount = 10;
let ranNum = generateRandomNumber();
while (guessCount > 0 ){
let guess = rls.question("Please take a guess \n");
if (guess == ranNum){
console.log("Congrats! You got it right!")
if (rls.keyInYN( "Would you like to play again?")){
gameLoop();
}else{
quitGame();
};
}else if ( guess < ranNum){
console.log("too low")
}else if( guess > ranNum){
console.log("too high")
}else{
console.log("Try that again")
continue;
}
guessCount--
console.log( "you have " + guessCount + " guesses left")
}
console.log( "You lose!")
quitGame();
return

}


/***
* Generates a random number
*
* @returns {number} - a number between 1 and 1000
*/
const generateRandomNumber = () => {

let randomNumber = 0
randomNumber = Math.floor(Math.random() * (1000 - 1) + 1);
return randomNumber
}

startGame()
Expand Down
Loading