Reiterate list of inquiries using InquirerJS

How can the questions be reset or have a specific answer lead to another previous question?


        var questions = [{
            {
                name: 'morefood',
                message: 'Do you want more food?',
                type: 'list',
                choices: [ 'Yes', 'No'],
            },{
                name: 'choiceoffood',
                message: 'Which food do you want more of?',
                type: 'list',
                choices: [ 'Hamburgers', 'Fries', 'Hotdogs']
                when: function(answers) {
                    return answers.morefood === 'Yes';
                }
            }, {
                name: 'quantityoffood',
                message: 'How much more do you want?',
                type: 'input',
                when: function(answers) {
                    return answers.quantityoffood === 'Yes';
                }
            },{
                name: 'confirmfood',
                message: 'Do you still want more food?',
                type: 'list',
                choices: [ 'Yes', 'No'],   <=========== if yes then goes back to choiceoffood
            }, 
        ]
    

Answer №1

While it may seem a bit unconventional, the best approach is to manage this within your application's logic. Here is an example:

const query1 = {
    type: 'text',
    message: 'Do you enjoy eating fruit?',
    name: 'query1',
}

const query2 = {
    type: 'text',
    message: 'What is your favorite fruit?',
    name: 'query2',
}

const query3 = {
    type: 'text',
    message: 'Which candy do you like the most?',
    name: 'query3',
}

inquiry
    .question(query1)
    .then(response => {
        if (response.query1 === 'yes') {
           return inquiry.question(query2)
        } else {
           return inquiry.question(query3)
        }
    })
    .then(response => {
        if (response.query2) {
             return console.log(response.query2, 'is a fantastic choice of fruit');
        } 
        if (response.query3) {
            return console.log(response.query3, 'is a wonderful option for candy');
        }
    })

UPDATE:

Upon further review of the documentation, using 'when' appears to be the appropriate solution in this scenario.

when: (Function, Boolean) This function receives the current user answers hash and should return true or false based on whether the question should be asked. It can also simply return a boolean value.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it necessary to test the $routeProvider when() configuration to ensure the stability of the application during runtime?

$routeProvider.when('/login', { templateUrl: 'app/login.html', controller: 'LoginController as vm' }); After switching vm to another variable like avm, the code passes unit tests successfully but results in a broken v ...

Discover the technique of accessing HTML/CSS toggle switch value in Golang

I am attempting to incorporate a toggle switch into my webpage. I followed this specific tutorial for guidance: w3schools.com Currently, I have set up my HTML file with a button and the toggle switch. Additionally, I configured my web server in Go to lis ...

Show full-screen images on click using Ionic framework

Currently, I am working on developing a mobile app using the Ionic framework. I have created a layout that resembles the one shown in this Card Layout. My question is: How can I make the card image display in full screen when clicked by the user and retur ...

How can I display table rows along with their child table rows in Angular?

Is there a way to display API data in a table using Angular? The table should have collapsible rows with nested child rows. This is the JSON file structure: { "collapse1": [ { "name": "Soil", "budget": 12345, "child": [ { ...

Creating a specialized pathway with variable inputs within the URL

As a Node beginner, I am facing a challenge with an Express exercise on dynamic routes. The task at hand is to create a route that can accept dynamic arguments in the URL path and respond with a quote from the respective author. Here's a snippet of th ...

Steps for removing an element from an array using Mongoose and Node.js

After reading and attempting to implement the solutions provided by others, I am still struggling to understand why it's not working for me. This is my first project involving backend development. While progressing through a course, I decided to work ...

Having trouble with the filtering feature in Material UI Datagrid

I'm currently using Material UI Data Grid to display a table on my website. The grid has an additional filter for each column, but when I click on the filter, it hides behind my Bootstrap Modal. Is there a way to bring it to the front? https://i.stac ...

In JavaScript, alert a message once all images have been clicked

I'm encountering a small issue with my javascript code. I am developing a game for a school project where the objective is to click (remove) fish using a fishing rod. However, the game does not have an end condition set up, so players cannot win. Belo ...

ZK: maintaining session connectivity

When I need to redirect to a tel:**** link in ZK and then redirect the user to another page after the call, I encounter a problem. Every time I click on the link, ZK interprets it as leaving the browser, which results in my client session ending automatica ...

The Firebase 'not-in' operator is malfunctioning

Within my database, there is a document located at: https://i.stack.imgur.com/OTZpd.png I am attempting to query the number of messages documents where the user's ID does not appear in the "read_by" array. This is the code I am currently using: const ...

The Array map function is not displaying the list within a React component that is based on a Class

I am having trouble displaying a list of food items in my Parent component FoodBox.js and its child component FoodItems.js. I am using the map() method, but the <ul> element is showing up empty. Here is my code for FoodBox.js const FOOD_ITEMS = [ { ...

There was an unexpected token syntax error while trying to assign the database URL from the environment variable "REACT_APP

I am facing an issue with setting an environment variable in my mongo.js file. Here is the code snippet where I have set the ENV variable: const mongo = require('mongodb').MongoClient; const assert = require('assert'); const ObjectId = ...

The Lightbox containing an IFrame is experiencing flickering issues when links are clicked

I am experiencing a similar issue with this post: How can I resolve flickering in IFrames? Unfortunately, I have yet to find a solution (and I'm worried about receiving negative ratings too :) ). Due to the fact that my website is on an intranet, I ...

The pages are constantly showing an error message that reads, "There is a problem with the SQL syntax in your code."

I am having trouble with my login page as it is displaying an error on my index.php file. Below is the code snippet from my index.php: <?php include('supsrwk_epenyenggaraan.php'); ?> <?php if (!function_exists("GetSQLValueString")) { f ...

Ways to Extract HTML DOM Attributes using Jquery

I am working with an HTML DOM that is saved in a JavaScript Variable. I need to figure out how to retrieve its attributes. For example: <script> //element_html is ajax response var element_html = '<h1 style="" class="app-control " data-order ...

Issue encountered with asynchronous execution while utilizing AWS Cognito registration process

I am developing a user management system using Amazon Web Services called Cognito. Everything works fine locally, but I encounter issues when running it on a Wamp server. I cannot seem to pinpoint the cause... could it be due to asynchronous execution? ...

Steps for modifying a cell within a table using a button click

Hello, I am currently working on a project where I want to display a specific div portion when a user clicks on an image icon within a table. However, I am encountering an issue with the JavaScript code as it only shows the div portion in the first row and ...

How to Implement Autocomplete Feature in Angular

CSS <div class="dropdown"> <input type="text" formControlName="itemName" (ngModelChange)="filterItems()" class="dropdown-input" (keyup)="onKeyPress($event)" (blur)="toggleDropdown(0)" (focus)="toggleDropdown(1)" placeholder="Search..." [ngCla ...

Create data structures in python, php, c#, go, c++, and java using a JavaScript/JSON object

I am looking for a way to reverse the output of the JSON.stringify function specifically for node.js. My goal is to generate code snippets for various programming languages by converting JavaScript structures into their native counterparts in python, PHP, ...

Having trouble activating the submit function using an external JavaScript file

Having trouble getting the form to submit properly. I have placed the form inside a <div id="access"></div> by setting its innerHTML using an included js file in the header of the page. Here's how it looks: <div id="access"> < ...