我正在忙着编写一个node.js程序,它涉及在不同的“菜单”之间进行切换。

时间:2018-08-06 10:30:02

标签: javascript node.js

切换和跟踪此问题最简单的方法是什么?例如主菜单,然后是子菜单。我有一个想法,涉及到称为状态的第三个“类”,它可以跟踪选择了哪个菜单,然后允许切换/重新发送菜单/功能,但是我想知道是否有更简单的方法来做到这一点? >

//first part of the program, the main menu which calls functions.

var app = require("./toDoListMain");
var states = require("./States");
var readline = require('readline');

var rl = readline.createInterface(process.stdin,process.stdout);

console.log("1. Add a list"+'\n' + "2. Select list" + '\n' + "3. Remove a list" + '\n'+ "4. View lists "+ '\n' + "5. Exit");

rl.question("What do you want to do?", function(firstAnswer){

    if(firstAnswer == 1){
        app.addName();
    }
    else if(firstAnswer == 2){
        app.showList();
        app.selectList();
    }
    else if(firstAnswer == 3){
        app.selectList();
        app.removeList();   
    }
    else if (firstAnswer == 4){
        app.showList();

    }

    else {
        rl.close;
        process.exit();
    }


});

//second part of the program, the functions

var readline = require('readline');


// var rl = readline.createInterface({
    // input: process.stdin,
// output: process.stdout});

    var toDoList = ['General', 'Daily','weekly'];

    var list = [

          this.addName = function(name){
            rl.question("What is the list's name?", function(answer){
                    var listName = answer;
                    console.log("List's name: " + listName);
                    toDoList = toDoList.concat(listName);
                    console.log(toDoList);
                    })

                    return toDoList;},

                    //console.log(animals.push('cows'));
                        //console.log(numLists.push(listName));

         this.selectList = function(list){

            console.log("You chose 2"); 
            console.log(toDoList);
            rl.question("Which list do you pick? 1-"+ toDoList.length, function(answer){
            var chosenList = toDoList[answer];});
            console.log("You have chosen: " + toDoList[answer]);
            return toDoList[answer];
            },
        this.removeList = function(remove){

            console.log("You chose 3"); 
            toDoList.remove[answer];
            return toDoList;
                },

        this.showList = function(show){
                        console.log("You chose 4");

                        console.log(toDoList);                          
                    },

        this.goOut = function(quit){
                console.log("You chose 5");
                return "quitting";
                rl.close();             
                process.exit(); 

            }
    ];


exports.app = list;

//then finally i will have a third part which is the states coding.

0 个答案:

没有答案