My friend created a simplistic country guessing game program in python, and having never messed with user input in JavaScript, I attempted to recreate it using the prompt library on npm.
As I got into it, ended up googling "promises inside of promises," and realized I probably had no idea what I was doing, I knew I needed to look somewhere else for help.
With the asynchronous nature of JavaScript, how would I create a command line game where it asks you to guess x number of countries based on y number of hints for each country. For example, it chooses a country and says the first hint, if the person can't guess the country on the first hint, it shows the second, and so on.
My following code sucks and barely works, but I'll give it just to show I tried.
let createCountry = (name, hints) => { return { name, hints } }
let countries = [
createCountry("america", ["my home!"]),
createCountry("not america", ["not my home!"])
]
for (let i = 0; i < countries.length; i++) {
new Promise(function(resolve, reject) {
let count = 0;
let country = countries[i]
console.log(country.name)
for (let h = 0, hint = country.hints[h]; h < country.hints.length; h++) {
let result = new Promise(function(resolve, reject) {
prompt.get(["country"], (err, result) => {
console.log(`Hint ${h + 1}/${country.hints.length}: ${hint}`)
if (result === country.name) {
console.log("You guessed correctly")
resolve(true)
} else {
console.log("You did not guess correctly")
reject(false)
}
})
})
}
console.log("\n")
})
}