注册 登录
编程论坛 JavaScript论坛

新手初來乍到,請各位大大幫我看看我寫的這個程序里錯誤在哪裡?

呂奉先生 发布于 2018-01-08 22:16, 1678 次点击
各位大大好,我學JS才一周左右,是個純菜鳥,最近遇見一道習題,是讓我自建一個名冊,有一個瀏覽功能(1),和一個退出功能(0)。下面是我寫的語句,但是無論我輸入0 以外的任何數字,他都會當成 1 來處理,所以請大大們幫我看看是哪裡出的問題,謝謝啦:

只有本站会员才能查看附件,请 登录
3 回复
#2
cho12018-01-11 16:20
            console.log('welcome to your contacts manager');
            var options = ["1: List contacts","0: Quit"];


            console.log(options[0]);
            console.log(options[1]);

            

            function enter_your_option() {
                number = Number(prompt("enter your option! 1 or 0"));
                if(number == 1) {
                    console.log("here is the list of all your contacts:");
                    var Contact = {
                        init: function (lastname,firstname) {
                            this.lastname = lastname;
                            this.firstname = firstname;
                        },

                        describe: function () {
                            var description = "lastName: " + this.lastname + "," + "fristname: "  + this.firstname + ",";
                            return description;
                        }

                    };
                    var contact1 = Object.create(Contact);
                    contact1.init("Smith","john");

                    var contact2 = Object.create(Contact);
                    contact2.init("doe","jane");

                    var contacts = [];
                    contacts.push(contact1);
                    contacts.push(contact2);

                    contacts.forEach(function (contact) {
                        console.log(contact.describe());
                    });

                    enter_your_option();
                }
                else if(number == 0){
                    console.log("end of contact manager!");
                }
            }
            enter_your_option();
#3
cho12018-01-11 16:21
1.
if(number == 1) {   //這裡要  == 或者 === ,一個 = 是賦值,所以程序來到這裡number就會被賦值為 1 ;

#4
cho12018-01-11 16:22
如果這些對你有幫助請告訴我,謝謝
1