2017-07-14 83 views
0

我是新来的JavaScript和试图完成什么应该是一个简单的任务,让用户与简单的联系人数组进行交互,我努力寻找正确的语法来循环选择变量,直到用户输入0退出,我已经尝试了while循环和,但只是不知道究竟在何处我应该把循环和建议将不胜感激与开关的Javascript查询

var Contact = { 
    //Initialise the contact 
    init: function(firstName, lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    }, 
    //Display the contact details 
    describe: function() { 
     var description = "Last name : " + this.lastName + ", first name: " + this.firstName; 
     return description 
    } 
}; 

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

var contact2 = Object.create(Contact); 
contact2.init("Jane", "Doe"); 

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


console.log("Welcome to your contacts manager!"); 
console.log("1: List contacts"); 
console.log("2: Add a contact"); 
console.log("0: Quit"); 


var choice = ""; 
choice = prompt("Enter 1, 2 or 0"); 

//while (choice !== "0") { 

switch (choice) { 
case "1": 
    console.log("Here's the list of all your contacts:"); 
    contacts.forEach(function (contact) { 
    console.log(contact.describe()); 
    }); 
    console.log("1: List contacts"); 
    console.log("2: Add a contact"); 
    console.log("0: Quit"); 
    break; 

case "2": 
    contacts.push(prompt("Enter new name: ")); 
    console.log("Contact added"); 
    console.log("1: List contacts"); 
    console.log("2: Add a contact"); 
    console.log("0: Quit"); 
    break; 

case "0": 
    console.log("Goodbye"); 
    console.log("1: List contacts"); 
    console.log("2: Add a contact"); 
    console.log("0: Quit"); 
    break; 

default: 
    console.log("Invalid Entry"); 
    console.log("1: List contacts"); 
    console.log("2: Add a contact"); 
    console.log("0: Quit"); 
    break; 
    } 

回答

1

请查看更新的while循环

var Contact = { 
 
    //Initialise the contact 
 
    init: function(firstName, lastName) { 
 
     this.firstName = firstName; 
 
     this.lastName = lastName; 
 
    }, 
 
    //Display the contact details 
 
    describe: function() { 
 
     var description = "Last name : " + this.lastName + ", first name: " + this.firstName; 
 
     return description 
 
    } 
 
}; 
 

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

 
var contact2 = Object.create(Contact); 
 
contact2.init("Jane", "Doe"); 
 

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

 

 
console.log("Welcome to your contacts manager!"); 
 
console.log("1: List contacts"); 
 
console.log("2: Add a contact"); 
 
console.log("0: Quit"); 
 

 
var choice = ""; 
 
while(choice != "0"){ 
 

 
choice = prompt("Enter 1, 2 or 0"); 
 

 
switch (choice) { 
 
case "1": 
 
    console.log("Here's the list of all your contacts:"); 
 
    contacts.forEach(function (contact) { 
 
    console.log(contact.describe()); 
 
    }); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 

 
case "2": 
 
    contacts.push(prompt("Enter new name: ")); 
 
    console.log("Contact added"); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 

 
case "0": 
 
    console.log("Goodbye"); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 

 
default: 
 
    console.log("Invalid Entry"); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 
    } 
 
}

更新

var Contact = { 
 
    //Initialise the contact 
 
    init: function(firstName, lastName) { 
 
     this.firstName = firstName; 
 
     this.lastName = lastName; 
 
    }, 
 
    //Display the contact details 
 
    describe: function() { 
 
     var description = "Last name : " + this.lastName + ", first name: " + this.firstName; 
 
     return description 
 
    } 
 
}; 
 

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

 
var contact2 = Object.create(Contact); 
 
contact2.init("Jane", "Doe"); 
 

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

 

 
console.log("Welcome to your contacts manager!"); 
 
console.log("1: List contacts"); 
 
console.log("2: Add a contact"); 
 
console.log("0: Quit"); 
 

 
var choice = ""; 
 
while(choice != "0"){ 
 

 
choice = prompt("Enter 1, 2 or 0"); 
 

 
switch (choice) { 
 
case "1": 
 
    console.log("Here's the list of all your contacts:"); 
 
    contacts.forEach(function (contact) { 
 
    console.log(contact.describe()); 
 
    }); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 

 
case "2": 
 
\t firstname = prompt("Enter first name: "); 
 
\t lastname = prompt("Enter last name: ") 
 
    var contactnew = Object.create(Contact); 
 
\t contactnew.init(firstname, lastname); 
 
\t contacts.push(contactnew); 
 
    console.log("Contact added"); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 

 
case "0": 
 
    console.log("Goodbye"); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 

 
default: 
 
    console.log("Invalid Entry"); 
 
    console.log("1: List contacts"); 
 
    console.log("2: Add a contact"); 
 
    console.log("0: Quit"); 
 
    break; 
 
    } 
 
}

+0

我可以问你一个关于这个while循环的问题,它通过第一次工作正常,但如果我使用push添加联系人,然后尝试列出数组失败,因为它说contact.describe不再是一个功能? –

+0

是的,所以它是在你的代码中,你尝试初始化2个值,并在输入你的名字作为单一值,没有问题我已经更新你的代码,请现在尝试请参阅更新的部分。 –

+0

谢谢你,现在有道理 –

-2

在你检查字符串“0”开关的情况下。用例0:或者将输入转换为字符串并传递数据进行切换。

+4

的输入字符串格式...':)'PS:不是我的downvote。 –

0

你希望用户输入多个的东西,所以你可以将迅速进入循环:

var choice = ""; 
while (choice !== "0") { 
    choice = prompt("Enter 1, 2 or 0"); 
    switch(choice){ 
    //... 
    } 
}