0

Writting a program using javascript and inquirerJS to ask question and play game, if dont want to play ok end game, if want to play more( ask for guessed number and show computer generated number greet if correct else better luck next time)

I tried solving and solved much but have technical problems related program logic and inquirerJS, Just run this code in your editor and you will know the error its logical and i dont know how to solve it.

`

import inquirer from 'inquirer';
const question =[
    {
      type : 'input' ,
      name : 'first_question' ,
      message : ' Are you ready to play this number guessing game ? \n If yes(Y) otherwise No(N) '
    },
    {
      type: 'list',
      name: 'input',
      message :  "So what's your guessed number   ? ",
      choices: ['1','2','3','4','5','6','7','8','9','10']
    }
  ] 
console.log("-------------------------------");
inquirer
.prompt(question)
.then((answers) => {
    const question1  = answers.first_question ;
    const choices = answers.input;
    // const question2  = answers.second_question ;
    let x = 0;
    x = getRandomInt();
    function getRandomInt() {
        return Math.floor(Math.random() * 10);
    }
    if(question1=== 'Y' || question1==='y'){
        console.log(`You choose this ${choices}`)
        if(choices == x){
        console.log(`Great yor answer is ${x} and its correct `)
        }else{
            console.log(`Your guessed ${choices} and computer generated  number is ${x}  \nBetter luck next time   ` )
        }
    }else if(question1==='N'|| question1 === 'n'){
        console.log("If you don't want to play OK")
    }
}) 


`

no iam
  • 1
  • 2

1 Answers1

0

The problem is in the sequence of the questions.

I'll fix the JavaScript code and I'll also improve it. First of all, we wrap the execution flow in an anonymous function with the async keyword so we can use the async and await syntax instead of then. We use destructuring to get the answer1 property of the object returned by the inquirer.prompt(...) method. We use match string method to check if the user input was 'y' or 'Y'. Also, I fixed the getRandomInt function; now it doesn't return a 0 as a possibility.

Try the following JavaScript code:

import inquirer from 'inquirer';

function getRandomInt() {
  return Math.floor(Math.random() * 9 + 1);
}

(async () => {
  const { answer1 } = await inquirer.prompt({
    type: 'input',
    name: 'answer1',
    message: ' Are you ready to play this number guessing game ? \n If yes(Y) otherwise No(N) '
  })

  if (!answer1.match(/[yY]/)) return console.log("It's OK if you don't want to play the game.")

  const { answer2 } = await inquirer.prompt({
    type: 'list',
    name: 'answer2',
    message: "So what's your guessed number   ? ",
    choices: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
  })

  const randomInt = getRandomInt()
  if (randomInt == answer2) return console.log(`Great! Yor answer is ${answer2} and it's correct.`)
  console.log(`Your guessed ${answer2} and computer generated number is ${randomInt}. Better luck next time.`)
})()