Hướng dẫn while loop with multiple conditions javascript - vòng lặp while với nhiều điều kiện javascript

2

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi muốn nhắc người dùng cho một đầu vào và nếu đó không phải là tùy chọn hợp lệ cho đến khi nhập hợp lệ.

Ngay bây giờ, nó luôn luôn tái cấu trúc không quan trọng đầu vào, đúng hay không.

Đây là mã:

var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
while (userChoice != "rock" || "paper" || "scissors" || "rope") {
    userChoice = prompt("Sorry invalid input, please enter either: rock, paper,scissors, or rope.");
}

Có vẻ như đó là một điều đơn giản để làm, tôi có hiểu lầm cách sử dụng trong khi các vòng không? Có lẽ các nhà khai thác đã sai? Không thể tìm thấy bất cứ điều gì hữu ích, bất kỳ trợ giúp nào được đánh giá cao!

Hướng dẫn while loop with multiple conditions javascript - vòng lặp while với nhiều điều kiện javascript

j08691

200K31 Huy hiệu vàng253 Huy hiệu bạc267 Huy hiệu Đồng31 gold badges253 silver badges267 bronze badges

Đã hỏi ngày 4 tháng 1 năm 2016 lúc 21:31Jan 4, 2016 at 21:31

1

Bạn cũng có thể sử dụng một mảng và indexof

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");

Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:39Jan 4, 2016 at 21:39

SMCDSMCDsmcd

3.0772 Huy hiệu vàng15 Huy hiệu bạc27 Huy hiệu đồng2 gold badges15 silver badges27 bronze badges

3

Cú pháp

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
1 là hợp lệ, nhưng hoặc sẽ liên kết muộn hơn so với kiểm tra bình đẳng, vì vậy về cơ bản bạn viết:

while ((userChoice != "rock") || "paper" || "scissors" || "rope") {
    //...
}

Bây giờ vì bạn sử dụng như vậy, về cơ bản bạn thực hiện kiểm tra hiệu quả và ít nhất

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
2 có hiệu quả. Tuy nhiên, bạn có thể có được hành vi bạn nhắm, bằng cách sử dụng kiểm tra rõ ràng và sử dụng toán tử
var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
3 thay vì
var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
4:

while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "rope") {
    //...
}

Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:39Jan 4, 2016 at 21:39

Hướng dẫn while loop with multiple conditions javascript - vòng lặp while với nhiều điều kiện javascript

SMCDSMCDWillem Van Onsem

3.0772 Huy hiệu vàng15 Huy hiệu bạc27 Huy hiệu đồng29 gold badges390 silver badges512 bronze badges

Cú pháp

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
1 là hợp lệ, nhưng hoặc sẽ liên kết muộn hơn so với kiểm tra bình đẳng, vì vậy về cơ bản bạn viết:

while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors" && userChoice != "rope") {

Bây giờ vì bạn sử dụng như vậy, về cơ bản bạn thực hiện kiểm tra hiệu quả và ít nhất

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
2 có hiệu quả. Tuy nhiên, bạn có thể có được hành vi bạn nhắm, bằng cách sử dụng kiểm tra rõ ràng và sử dụng toán tử
var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
3 thay vì
var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
4:Jan 4, 2016 at 21:36

Willem Van Onsemwillem Van OnsemHogan

412K29 Huy hiệu vàng390 Huy hiệu bạc512 Huy hiệu Đồng10 gold badges76 silver badges115 bronze badges

Trong khi Userchoice không bằng bất kỳ lựa chọn nào ... vì vậy và

var userChoice;
while (!~["rock", "paper", "scissors", "rope"].indexOf(userChoice)) {
    userChoice = prompt("Sorry invalid input, please enter either: rock, paper,scissors, or rope.");
}

Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:39Jan 4, 2016 at 21:39

Hướng dẫn while loop with multiple conditions javascript - vòng lặp while với nhiều điều kiện javascript

SMCDSMCDNina Scholz

3.0772 Huy hiệu vàng15 Huy hiệu bạc27 Huy hiệu đồng24 gold badges326 silver badges366 bronze badges

Cú pháp

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
1 là hợp lệ, nhưng hoặc sẽ liên kết muộn hơn so với kiểm tra bình đẳng, vì vậy về cơ bản bạn viết:ignoring case sensitivity), it is possible to use regular expressions, e.g. match:

while( ! userChoice.match(/^\s*(?:rock|paper|scissors|rope)\s*$/i)) { ... }

Bây giờ vì bạn sử dụng như vậy, về cơ bản bạn thực hiện kiểm tra hiệu quả và ít nhất

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
2 có hiệu quả. Tuy nhiên, bạn có thể có được hành vi bạn nhắm, bằng cách sử dụng kiểm tra rõ ràng và sử dụng toán tử
var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
3 thay vì
var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
4:

Willem Van Onsemwillem Van Onsem


412K29 Huy hiệu vàng390 Huy hiệu bạc512 Huy hiệu Đồng

var validChoices = {rock: true,
                    paper: true,
                    scissors: true,
                    rope: true},
    userChoice = ...
while ( ! validChoices.hasOwnProperty(userChoice)) {
  ...
}

Trong khi Userchoice không bằng bất kỳ lựa chọn nào ... vì vậy và

Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:36Jan 4, 2016 at 21:45

Hướng dẫn while loop with multiple conditions javascript - vòng lặp while với nhiều điều kiện javascript

HoganhoganAprillion

67.6K10 Huy hiệu vàng76 Huy hiệu bạc115 Huy hiệu đồng4 gold badges54 silver badges87 bronze badges

2

Có lẽ một mảng thông minh hơn để sử dụng với

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
5.

Nina Scholznina Scholz

361K24 Huy hiệu vàng326 Huy hiệu bạc366 Huy hiệu Đồngat least one of the operands is

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
8.

Đối với các quy tắc phức tạp hơn (chẳng hạn như bỏ qua độ nhạy của trường hợp), có thể sử dụng các biểu thức thông thường, ví dụ: cuộc thi đấu:

userChoice = "rock";
userChoice != "rock" => FALSE

.

"rock" != "paper" => TRUE 

Lưu ý: Cảm ơn Willem Van Onsem vì đã đề xuất Regex

Cũng có khả năng là những người theo nghĩa chữ:

hoặc ES6 đặt cho các trình duyệt mới nhất kể từ năm 2016:

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
6at least one of the operands evaluates to
while ((userChoice != "rock") || "paper" || "scissors" || "rope") {
    //...
}
5.

Đã trả lời ngày 4 tháng 1 năm 2016 lúc 21:45

userChoice = "rock";
userChoice != "rock" => FALSE

Aprillionaprillion

20.4K4 Huy hiệu vàng54 Huy hiệu bạc87 Huy hiệu đồngFeb 21, 2019 at 8:37

Tôi sẽ thử một câu trả lời chung hơn:ruslaniv

Vòng lặp

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
7 chạy trong khi bất cứ điều gì trong ngoặc là
var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");
8.1 silver badge12 bronze badges

Một vòng lặp trong thời gian có thể có hai điều kiện JavaScript?

Cú pháp. Cú pháp chung của vòng lặp trong thời gian trong JavaScript được đưa ra dưới đây. Ở đây, điều kiện là một tuyên bố. Nó có thể bao gồm nhiều câu chuyện phụ, tức là có thể có nhiều điều kiện.there can be multiple conditions.

Bạn có thể có 2 điều kiện trong một vòng lặp không?

Sử dụng nhiều điều kiện như được thấy trên dòng 4, vòng lặp trong khi có hai điều kiện, một điều kiện sử dụng toán tử và một điều khiển sử dụng hoặc toán tử.Lưu ý: Điều kiện và điều kiện phải được đáp ứng cho vòng lặp chạy.Tuy nhiên, nếu một trong hai điều kiện ở phía hoặc bên của toán tử trả về true, vòng lặp sẽ chạy.the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.

Chúng ta có thể sử dụng && trong vòng lặp không?

Việc sử dụng toán tử logic trong trong khi loop -sử dụng và (&&), điều đó có nghĩa là cả hai điều kiện phải là đúng.- hoặc toán tử (||), vòng lặp này sẽ chạy cho đến khi cả hai điều kiện trở lại sai.- Ở đây chúng tôi đang sử dụng hai toán tử logic không (!) Và (&&).using AND(&&) operator, which means both the conditions should be true. – OR(||) operator, this loop will run until both conditions return false. – Here we are using two logical operators NOT (!) and AND(&&).

Làm thế nào để bạn thêm điều kiện vào vòng lặp trong một thời gian?

Bạn bắt đầu vòng lặp với từ khóa WHRE, sau đó đặt điều kiện thành bất kỳ biểu thức có điều kiện nào.Một biểu thức có điều kiện là một tuyên bố đánh giá là đúng hoặc sai.Miễn là điều kiện là đúng, phần thân vòng (khối được thụt vào sau) sẽ thực hiện bất kỳ câu lệnh nào mà nó chứa.initiate the loop with the while keyword, then set the condition to be any conditional expression. A conditional expression is a statement that evaluates to True or False . As long as the condition is true, the loop body (the indented block that follows) will execute any statements it contains.