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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Guessing Game Lab

Build a guessing game in the command line!
Build a guessing game in the command line!!!

Similar to the lesson, you will be building a guessing game! The main difference with this lab is the computer will generate a random number and it's up to you to guess it.

Expand Down
47 changes: 43 additions & 4 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ const rls = require('readline-sync')
* @returns {undefined}
*/
const startGame = () => {

console.log("Hello, welcome to our game")
if (rls.keyInYN("Would you like to play a game with us?")) {
gameLoop();
console.log("Let's start!")
} else {
quitGame();
console.log("Have a nice life!")
}
}


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

console.log("Goodbye");
process.exit()
console.log("Goodbye!");
process.exit()
}

/**
Expand All @@ -28,17 +39,45 @@ const quitGame = () => {
* @returns {undefined}
*/
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 randomNumber = generateRandomNumber()
let guess
while (guessCount > 0) {
guess = rls.questionInt("Can you guess the number on my mind? \n")
guessCount--;
if (guess === randomNumber) {
console.log("Congrats! You got it right!")
if (rls.keyInYN("Do you want to play again?")) {
gameLoop()
} else {
quitGame()
}
} else if (guess > randomNumber) {
console.log("Your guess is too high")
} else {
console.log("Your guess is too low")
}
}
console.log("You lose!")
quitGame()
}






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

let randomNum = Math.floor(((Math.random() * 1000) + 1));
return randomNum
}

startGame()
Expand Down
Loading