2017-07-14 76 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){ 
    //... 
    } 
}