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 @@ -4,7 +4,7 @@

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.
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.

## Learning Objectives

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

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

/**
Expand All @@ -17,7 +23,8 @@ const startGame = () => {
* @returns {undefined}
*/
const quitGame = () => {

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

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

guessCount = 10
console.log("I have a number in mind")
console.log("It's between 1 and 1000")
console.log("You have 10 guesses")
const randomNum = generateRandomNumber();
while (guessCount > 0){
guessNum = rls.questionInt("What is the number you guess? ")
if (guessNum === randomNum){
console.log(`Congratulations! You got it right! The number I guessed was ${guessNum}`)
if (rls.keyInYN("Would you like to play again? ")){
console.clear();
gameLoop();
} else{
quitGame();
}
} else if (guessNum > randomNum){
console.log("Your guess is too high.")
guessCount--
console.log(`You have ${guessCount} guesses left.`)
} else if (guessNum < randomNum){
console.log("Your guess is too low.")
guessCount--
console.log(`You have ${guessCount} guesses left.`)
}
if (guessCount === 0){
console.log("You lose!")
quitGame();
}
}
}


Expand All @@ -38,6 +73,7 @@ const gameLoop = () => {
* @returns {number} - a number between 1 and 1000
*/
const generateRandomNumber = () => {
return Math.floor(Math.random() * 1001)

}

Expand Down
Loading