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
45 changes: 38 additions & 7 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ const rls = require('readline-sync')
* @returns {undefined}
*/
const startGame = () => {

}

if(rls.keyInYN("Ready to play? 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 = () => {

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

/**
Expand All @@ -28,7 +34,28 @@ 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")
const answer = generateRandomNumber()
let guess =rls.question()
for (let i =1; i <= 10; i++)
if(guess === answer) {
console.log("Congrats! You got it right!")
break;
}
else if(guess > answer) {
console.log("Your guess is too high")
gameLoop()
}
else if(guess < answer) {
console.log("Your guess is too low")
gameLoop()
}
//if guesses exceed 10
else
console.log("You lose!")
quitGame()
}


Expand All @@ -38,8 +65,12 @@ const gameLoop = () => {
* @returns {number} - a number between 1 and 1000
*/
const generateRandomNumber = () => {
let min = 1;
let max = 1000;
return Math.random() * (max - min) + min;
}


}

startGame()

Expand Down
Loading