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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ $ node game.js 15 100 500 # 15 tries, 100 min, 500 max
## Resources

- [how to parse command line arguments](https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/)
Hi
47 changes: 47 additions & 0 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ const rls = require('readline-sync')
* @returns {undefined}
*/
const startGame = () => {
if(rls.keyInYN("Do you want to play a game?")) {
console.log("Let's start!")
gameLoop()
}
else {
console.log("Have a nice life!")
quitGame()
}


}

Expand All @@ -17,6 +26,8 @@ const startGame = () => {
* @returns {undefined}
*/
const quitGame = () => {
console.log("Goodbye!")
process.exit()

}

Expand All @@ -28,17 +39,53 @@ 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 randomNum = generateRandomNumber()
let guess;


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while loop would be better. While count > 0. Then you can decrement count depending on the result

for (let count = 10; count > 0; count--) {
let guess = rls.questionInt("what is your guess? \n")
if (guess === randomNum) {
console.log("Congrats! you got it right")
if (rls.keyInYN("Do you want to play again? \n")) {
gameLoop()
}
else {
quitGame()
}
}
else if( guess > randomNum) {
console.log("Your guess is too high")
}
else if(( guess < randomNum)) {
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 = () => {



return Math.floor((Math.random() * 1000) + 1 )

}

startGame()
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
"jest": "^26.6.1",
"mock-stdin": "^1.0.0",
"readline-sync": "^1.4.10"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/carloecheverri/Guessing-Game-cli-lab.git"
},
"bugs": {
"url": "https://github.com/carloecheverri/Guessing-Game-cli-lab/issues"
},
"homepage": "https://github.com/carloecheverri/Guessing-Game-cli-lab#readme"
}