For loop 如何简化Javascript中的简单循环?
我现在正在尝试一段时间,让它变得完美。我正试图简化我创建的for循环,使其实际工作,不需要任何数组,只需要最基本的JavaScriptFor loop 如何简化Javascript中的简单循环?,for-loop,For Loop,我现在正在尝试一段时间,让它变得完美。我正试图简化我创建的for循环,使其实际工作,不需要任何数组,只需要最基本的JavaScript for (var x=0;x<=1;x++) { if (secondInput == luckyNumber || secondInput == luckyNumber2 || secondInput == luckyNumber3) { if (thirdInput == luckyNumber || thirdInput
for (var x=0;x<=1;x++) {
if (secondInput == luckyNumber || secondInput == luckyNumber2 || secondInput == luckyNumber3) {
if (thirdInput == luckyNumber || thirdInput == luckyNumber2 || thirdInput == luckyNumber3) {
if (firstInput == luckyNumber || firstInput == luckyNumber2 || firstInput == luckyNumber3) {
while (firstInput !== secondInput){
while(firstInput !== thirdInput){while(secondInput !== thirdInput) {
alert('Congratulations! You got all 3 numbers correct. You\'ve won £1,000!');
}
}
}
}
}
}
这个代码有意义吗?还是我做错了什么?我有一种感觉,我甚至可以省略这个循环,但这是我认为正确的唯一方法 编写一个函数,接收输入,将其与幸运数字进行比较,并返回一个布尔值和结果 在if子句中调用该函数
我不太明白您想用while循环做什么。您可以尝试使用这个想法来帮助:
[1, 3, 2].sort()
将您的问题和答案存储在数组中,并对它们进行排序,然后进行比较。当然,检查javascript数组是否相等是一个有趣的新项目:给你。你说你想把它简化
for (var x = 0; 1 >= x; x++) {
if (!(secondInput != luckyNumber && secondInput != luckyNumber2 && secondInput != luckyNumber3 || thirdInput != luckyNumber && thirdInput != luckyNumber2 && thirdInput != luckyNumber3 || firstInput != luckyNumber && firstInput != luckyNumber2 && firstInput != luckyNumber3)) {
while (firstInput !== secondInput) {
while (firstInput !== thirdInput) {
while (secondInput !== thirdInput) {
alert("Congratulations! You got all 3 numbers correct. You\'ve won £1,000!");
}
}
}
}
}
如何打破内部while循环?