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

Searching for the nearest BBCode using JavaScript regex

After checking the suggested SO questions, none of them seem to address my issue. I have a small form textarea that allows for BBCODE formatting. For example: [url=http://www.derp.com/]link[/url] [url=http://www.derp.com/435]link[/url] When a link is hi ...

Arranging cards in a stack using VueJS

I am currently working with a small vue snippet. Originally, I had v-for and v-if conditions in the snippet, but due to issues reading the array, it is now hardcoded. The current setup produces three cards stacked on top of each other. I am exploring opti ...

Is there a way to dynamically register an external component in Vue3 without altering the main project?

In my current project, known as "ProjectMain," I am also working on another project that will act as an extension to ProjectMain - let's call it "MyComponent." My intention is to create MyComponent as a standalone library. My main question is: can I ...

Tips for accessing the most recent embedded document added using the push() method

I'm having difficulty determining the feasibility of this situation. While using the mongoose blog example to illustrate, my specific use case is a bit more complex: var Comments = new Schema({ title : String , body : String , date ...

Check the Full Calendar to see if any events are scheduled for today

I have a group of users who need to save their availability. Currently, I am utilizing Full Calendar and looking for a way to prevent them from adding the same event multiple times on a single day. My tech stack includes VueJs and all events are stored in ...

Toggle the visibility of an element with a single button click

Below is a snippet of my sample code: SAMPLE HTML CODE: <ul> <li class="item"> item 1 </li> <li class="item"> item 1 </li> <li class="item"> item 1 </li> <li class="item"> item 1 </li> <l ...

What is the best method for globally configuring the Angular moment timezone?

I integrated angular moment js into my angular application. I am looking to display the date and time based on the time zone of the user accessing the app. However, I am facing difficulty in applying the time zone globally throughout my application. https ...

Is there a way to retrieve all documents based on the start and end of a specific day?

One common issue I am facing involves submitting a date to my nodejs server in a specific format

 2018-11-02T00:36:00+05:30 // The actual time should be 12:36AM However, when examining the document in my database (using studio 3T), the format appear ...

"Trouble with Angular JS foreach loop: only the final item in the array

Struggling to loop through each item object in a JSON data set and evaluate its value using angular.forEach(). However, only the last item is being returned, making it impossible to perform any meaningful evaluation. Oddly enough, when using console.log(), ...

Exploring the world of Javascript: The significance of variable scope and its

Encountered a unique challenge while attempting to execute an ajax call and confine the function's actions to itself. Below is the code snippet: $(document).on('click', 'input.action', function(event) { var self = this; ...

Guide on creating a toggle effect for a div with querySelector and addEventListener

I utilized the email and password divs provided by Bootstrap. The CSS for the id dropdownlogin includes display: none; in this post, I hope that I have shared adequate information. <script> document.addEventListener('DOMContentLoaded', ...

Ensure that the vue-router guard waits for the completion of the axios API call in Vuex before proceeding

I am currently working with a django-rest-axios-vuejs application stack, and I have a specific task that involves the vue-router. Within the beforeEach guard of the vue-router, I am checking permissions by verifying something in the me object within the v ...

The functionality of React setState seems to be malfunctioning when activated

Having encountered an unusual issue with react's setState() method. Currently, I am utilizing Azure DevOps extensions components and have a panel with an onClick event that is intended to change the state of IsUserAddedOrUpdated and trigger the addOr ...

Executing JavaScript code within an AJAX request

Having a problem with an AJAX function that I need help solving: PAGE A represents the main page. PAGE X represents the content loaded via AJAX. RES A represents the results from PAGE A. RES B represents the new results loaded via AJAX. PAGE A initially ...

How can React and Redux ensure that response data is accessible to every component?

When using react and redux, how can data written in the useDispatch function be made available in other components? Additionally, how can the customerId be accessed in all components? I have created a code that calls an API and returns data in response. I ...

Which specific transitionend (or animationend) event should I use for this application?

I'm feeling a bit lost when it comes to using transitionend (or if I should be using animationend in this scenario). I'm not sure whether to utilize var, node, or box. Essentially, I am a complete beginner in this case. My goal is to have my div ...

Creating smooth animations in JavaScript without relying on external libraries such as jQuery

Is there a way in JavaScript to make a <div> slide in from the right when hovered over, without relying on jQuery or other libraries? I'm looking for a modern browser-friendly solution. const div = document.querySelector('.fro ...

Establish the dimensions of the element to match the dimensions of a responsive image

Currently, I am working on implementing flippable images on my website. It is important to me that the "back" of these images matches the dimensions of the front image. The challenge I am facing is that I have applied the Bootstrap img-responsive class to ...

Handling events with Angular and Firebase

I am currently developing a polling application where users can create polls (questions and responses) that will be stored in my Firebase database. The questions are then displayed as a list on the sidebar, which is working well so far. Now, I am facing a ...

JavaScript: Converting an array of strings into an array of objects with proper formatting

After scanning barcodes, I have an array of strings that currently contains the following data: var array = ['NEW', '1111', 'serial1', 'serial2, 'NEW', '2222', 'serial3', 'serial4'] ...