Created
July 2, 2020 23:22
-
-
Save andreiskandar/1805dbbf04f9d3f245317c9cffab4c1d to your computer and use it in GitHub Desktop.
Number guesser using prompt sync dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In this kata you'll be responsible for setting up your JS file from scratch. Make sure it is well organized! | |
Write a guessing game where the user has to guess a secret number. After every guess the program | |
tells the user whether their number was too large or too small. At the end, the number of tries | |
needed should be printed. | |
Inputting the same number multiple times should only count as one try. If the user provides an answer | |
which isn't a number, print an error message and do not count this as a try. | |
*/ | |
let prompt = require('prompt-sync')({sigint: true}); | |
let numberToGuess = Math.floor(Math.random() * 100) + 1; | |
let count = 0; | |
let foundCorrectNumber = false; | |
while(!foundCorrectNumber){ | |
let answer = prompt("Guess a number between 0 - 100: "); | |
answer = Number(answer); | |
if (isNaN(answer)){ | |
count++; | |
console.log('Not a number! Try again') | |
} else if(answer === numberToGuess){ | |
foundCorrectNumber = true; | |
count++; | |
console.log(`You got it! You took ${count} attempts`) | |
} else if(answer < numberToGuess){ | |
count++; | |
console.log('Too low!'); | |
} else if (answer > numberToGuess) { | |
count++; | |
console.log('Too high!'); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment