1

I'm trying to get some code to have a certain message to appear on the screen depending on what answer they give on a question. This is a node application that is using the Inquirer package and every-time that I run the node application, it comes back with "undefined".

{
        type: "checkbox",
        name: "channels",
        message: "Which of these TV channels would you watch?!",
        choices: ["Investigation Discovery", "CNN", "Fox News", "TLC"]
    }
]).then(function (responses) {
    for(let i = 0; i < responses.channels; i++) {
        if (responses.channels === 0) {
            console.log("You are probably smart");
        }`enter code here`
        else if (responses.channels === 1) {
            console.log("You are probably well informed");
        }
        else if (responses.channels === 2) {
            console.log("You are probably not very well informed");
        }
        else {
            console.log("You are probably an idiot");
        }
    }

As stated before, it is supposed to return a message in the console depending on what choice was chosen, but it only comes back with "undefined".

wtsegars
  • 93
  • 3
  • 15

2 Answers2

0

It is because you use the wrong type. Checkbox allow to select multiple response.

For your use case, you should use list or rawlist and an array of object the choices, to have a different value between what is displayed and what you want to use after.

So, something like that should be what you excpeted:

const inquirer = require("inquirer");


inquirer
  .prompt([
    {
        type: "list",
        name: "channels",
        message: "Which of these TV channels would you watch?!",
        choices: [{ name: "Investigation Discovery", value: 0 }, { name: "CNN", value: 1 }, { name: "Fox News", value: 2}, { name: "TLC", value: 3}]
    }
  ])
  .then((responses) => {
        if (responses.channels === 0) {
            console.log("You are probably smart");
        }
        else if (responses.channels === 1) {
            console.log("You are probably well informed");
        }
        else if (responses.channels === 2) {
            console.log("You are probably not very well informed");
        }
        else {
            console.log("You are probably an idiot");
        }
});
Heziode
  • 53
  • 1
  • 9
  • On which operating system are you ? I have updated the previous message to remove the for loop. Try with this new sample. – Heziode Apr 10 '19 at 19:57
  • Im using macOS Mojave. I did get a different response this time except it would only print "You are probably an idiot" no matter what choice I made – wtsegars Apr 10 '19 at 20:12
0

Okay so I managed to solve it myself. Here is what I needed to do:

    if (responses.channels = 1) {
        console.log("You are probably smart");
    }
    else if (responses.channels = 2) {
        console.log("You are probably well informed");
    }
    else if (responses.channels = 3) {
        console.log("You are probably not very well informed");
    }
    else {
        console.log("You are probably an idiot");
    }
wtsegars
  • 93
  • 3
  • 15