I am writing a simple program for a card game in node. As of right now, I am just testing it, so I am using CLI to take user input (using the prompt-sync lib). There is a small issue when I ask the user if they want to get another card, since we don't know how many times the user wants a card, I have wrapped the function asking for user input inside a while loop. The loop is as follows...
let player_score = getValues(deck_information.players_cards);
let dealer_score = getValues(deck_information.dealers_card);
let continue_asking = true;
while (getInput(`You currently have ${player_score} and the dealer has ${dealer_score}. Would you like to hit?`)) {
console.log(player_score);
console.log(dealer_score);
dealCards(deck_information.deck_id)
.then(addedValue => {
console.log("executes")
player_score += addedValue;
console.log(player_score);
})
.catch(err => {
console.log(err.message);
})
}
the "getInput" and "dealCards" method bodies areas follows...
const getInput = (text) => {
let guess = prompt(text);
if (guess === 'y' || guess === 'Y') {
return true;
}
return false;
}
const dealCards = async (deck_identifier) => {
const dealtCardsResp = await axios.get(`https://deckofcardsapi.com/api/deck/${deck_identifier}/draw/?count=1`);
return getValues(dealtCardsResp.data.cards);
}
'getValues' is just a function that checks the denomination of the card given and returns it as a number.
Problem Statement When I run this program, it deals the first two cards out and when prompts the user with the following message
You currently have ${player_score} and the dealer has ${dealer_score}. Would you like to hit?
Where player_score is the player's score and dealer_score is the dealers score.
This is all expected, however, when I enter 'y' to deal the next card, it simply prompts the user with the question again and again (each time the input 'y' is given)
when the user enters anything other than 'y', the console.log("executes")
code snippet runs and the program terminates.
I am fairly new to asynchronous JavaScript (and JavaScript in general) so please feel free to critique any "Best Practices" I may not be using and point out any logical errors that are causing the above issue.
Thanks in advance!