添加一个复合 AND 条件,允许循环仅在以下情况下继续迭代
Add a compound AND condition that allows the loop to continue to iterate only if
我在做运动,要求如下:
使用 For 循环遍历中奖号码数组中的每个位置,并将客户号码与数组包含的每个号码进行比较。
要完成此操作,您需要进行以下设置。
- 循环的计数器变量(例如 i)。
- 用于标记是否找到匹配项的布尔变量(例如匹配项)。
- 只允许循环继续迭代的复合AND条件
如果未找到匹配项,并且尚未到达数组末尾。
- 嵌套在检查客户的 For 循环中的 if 语句
数组中的每个中奖号码,每次循环
如果找到匹配项,则迭代并将布尔值匹配设置为真。
到目前为止,我所做的工作有效,但我不明白要求 3 会去哪里或需要它(因为 for 循环已经检查是否未到达数组末尾?所以肯定只需要单个 if 语句而不是复合语句?),有人可以解释一下吗?
我目前拥有的:
var customerNumbers = 12;
var winningNumbers = [];
var match = false;
// Adds the winning numbers to winningNumbers
winningNumbers.push(12, 17, 24, 37, 38, 43);
// Messages that will be shown
var winningMessage = "This Week's Winning Numbers are:\n\n" + winningNumbers + "\n\n";
var customerMessage = "The Customer's Number is:\n\n" + customerNumbers + "\n\n";
var resultMessage = "Sorry, you are not a winner this week.";
// Searches the array to check if the customer number is a winner
for (var i = 0; i < winningNumbers.length; i++) {
if (customerNumbers == winningNumbers[i]) {
resultMessage = "We have a match and a winner!"
match = true;
}
}
// Result
alert(winningMessage + customerMessage + resultMessage);
像这样在 for 条件中添加 and 语句。
for (var i = 0; i < winningNumbers.length && !match; i++) {
无需更改 if 语句
var customerNumbers = 12;
var winningNumbers = [];
var match = false;
// Adds the winning numbers to winningNumbers
winningNumbers.push(12, 17, 24, 37, 38, 43);
// Messages that will be shown
var winningMessage = "This Week's Winning Numbers are:\n\n" + winningNumbers + "\n\n";
var customerMessage = "The Customer's Number is:\n\n" + customerNumbers + "\n\n";
var resultMessage = "Sorry, you are not a winner this week.";
// Searches the array to check if the customer number is a winner
for (var i = 0; i < winningNumbers.length && !match; i++) {
if (customerNumbers == winningNumbers[i]) {
resultMessage = "We have a match and a winner!"
match = true;
}
}
// Result
alert(winningMessage + customerMessage + resultMessage);
我在做运动,要求如下:
使用 For 循环遍历中奖号码数组中的每个位置,并将客户号码与数组包含的每个号码进行比较。
要完成此操作,您需要进行以下设置。
- 循环的计数器变量(例如 i)。
- 用于标记是否找到匹配项的布尔变量(例如匹配项)。
- 只允许循环继续迭代的复合AND条件 如果未找到匹配项,并且尚未到达数组末尾。
- 嵌套在检查客户的 For 循环中的 if 语句 数组中的每个中奖号码,每次循环 如果找到匹配项,则迭代并将布尔值匹配设置为真。
到目前为止,我所做的工作有效,但我不明白要求 3 会去哪里或需要它(因为 for 循环已经检查是否未到达数组末尾?所以肯定只需要单个 if 语句而不是复合语句?),有人可以解释一下吗?
我目前拥有的:
var customerNumbers = 12;
var winningNumbers = [];
var match = false;
// Adds the winning numbers to winningNumbers
winningNumbers.push(12, 17, 24, 37, 38, 43);
// Messages that will be shown
var winningMessage = "This Week's Winning Numbers are:\n\n" + winningNumbers + "\n\n";
var customerMessage = "The Customer's Number is:\n\n" + customerNumbers + "\n\n";
var resultMessage = "Sorry, you are not a winner this week.";
// Searches the array to check if the customer number is a winner
for (var i = 0; i < winningNumbers.length; i++) {
if (customerNumbers == winningNumbers[i]) {
resultMessage = "We have a match and a winner!"
match = true;
}
}
// Result
alert(winningMessage + customerMessage + resultMessage);
像这样在 for 条件中添加 and 语句。
for (var i = 0; i < winningNumbers.length && !match; i++) {
无需更改 if 语句
var customerNumbers = 12;
var winningNumbers = [];
var match = false;
// Adds the winning numbers to winningNumbers
winningNumbers.push(12, 17, 24, 37, 38, 43);
// Messages that will be shown
var winningMessage = "This Week's Winning Numbers are:\n\n" + winningNumbers + "\n\n";
var customerMessage = "The Customer's Number is:\n\n" + customerNumbers + "\n\n";
var resultMessage = "Sorry, you are not a winner this week.";
// Searches the array to check if the customer number is a winner
for (var i = 0; i < winningNumbers.length && !match; i++) {
if (customerNumbers == winningNumbers[i]) {
resultMessage = "We have a match and a winner!"
match = true;
}
}
// Result
alert(winningMessage + customerMessage + resultMessage);