[英]Got stuck in an infinite loop
Please help here I wanna print in the console all array elements in condition that they are not numbers and they do not start with letter "A"请在这里帮忙我想在控制台中打印所有数组元素,条件是它们不是数字并且它们不以字母“A”开头
I think the problem is where to put the i += 1;我认为问题是放在哪里 i += 1; but actually when I am changing its position the output is not what like I need.
但实际上,当我更改其 position 时,output 并不是我所需要的。
I tried to do it with for and every thing turned out okay, but I don't know what is the problem with while.我试着用 for 来做,结果一切正常,但我不知道 while 有什么问题。
Here is the code:这是代码:
let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"];
let i = 0;
while (i < friends.length) {
if (friends[i][0] === "A" || typeof friends[i] === "number") {
continue;
}
console.log(friends[i]);
i += 1;
}
I tried to use while to do what I've said previously我试着用 while 来做我之前说过的
Since you already have an array, a better approach for this would be to just loop through it.由于您已经有了一个数组,因此更好的方法是循环遍历它。 That way you could avoid
while
loop entirely.这样你就可以完全避免
while
循环。
let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"]; const filteredFriends = friends.filter(friend => friend[0];== 'A' && typeof friend.== 'number'); console.log(filteredFriends);
documentation for filter() 过滤器()的文档
The reason you were getting an infinite loop is because of the continue
statement inside if
.您获得无限循环的原因是因为
continue
里面的语句if
。 That skips the rest of the code for that iteration and i += 1
doesn't get executed.这会跳过该迭代代码的 rest,并且
i += 1
不会被执行。
When you continue
, you do not increment i
, so in next iteration i
stays the same.当您
continue
时,您不会增加i
,因此在下一次迭代中i
保持不变。
You can always use for... of
to drop need for manually incrementing i
您始终可以使用
for... of
来减少手动递增i
的需要
let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"]; for (const friend of friends) { if (typeof friend === "number" || friend[0] === "A") { continue; } console.log(friend); }
You need to increase i
every time you loop or your while condition will never resolve (unless all meet your condition).您需要在每次循环时增加
i
,否则您的 while 条件将永远无法解决(除非都满足您的条件)。
while (i < friends.length) {
if (friends[i][0] !== "A" && typeof friends[i] !== "number") {
console.log(friends[i]);
}
i += 1;
}
* I changed the condition to look for the desired result rather than the negative (just a personal preference). *我改变了条件来寻找想要的结果而不是消极的(只是个人喜好)。
Please see the changes in code.This will work as expected.请查看代码中的更改。这将按预期工作。
let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"];
let i = 0;
while (i < friends.length) {
if (friends[i].toString().charAt(0).toLowerCase() !=='a' &&
typeof(friends[i]) !== 'number') {
console.log(friends[i]);
}
i++;
}
The code enters into an infinite loop because it won´t increment the i variable inside the if and before the continue.代码进入无限循环,因为它不会在 if 内和继续之前递增 i 变量。
Your code can be easily fixed like this您的代码可以像这样轻松修复
let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"];
let i = 0;
while (i < friends.length) {
if (friends[i][0] === "A" || typeof friends[i] === "number") {
i += 1
continue;
}
console.log(friends[i]);
i += 1;
}
try this, hope this help you试试这个,希望这对你有帮助
let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"];
for (let i = 0; i < friends.length; i++) {
if (friends[i][0] === "A" || typeof friends[i] === "number") {
continue;
}
console.log(friends[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.