陷入了无限循环

发布于 2025-01-18 05:10:18 字数 482 浏览 0 评论 0原文

请帮忙 在这里,我想在控制台中打印所有数组元素,条件是它们不是数字,并且不是从字母“ a”开始的,

我认为问题是将i += 1放在哪里; 但是实际上,当我改变其位置时,输出不是我需要的。

我试图做到这一点,一切都可以,但是我不知道有什么问题。

这是代码:

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;
}

我试图使用以前说过的事情

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"

I think the problem is where to put the i += 1;
but actually when I am changing its position the output is not what like I need.

I tried to do it with for and every thing turned out okay, but I don't know what is the problem with 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼泪都笑了 2025-01-25 05:10:18

由于您已经有一个数组,因此更好的方法就是循环通过它。
这样,您可以完全避免完全循环。

let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"];

const filteredFriends = friends.filter(friend => friend[0] !== 'A' && typeof friend !== 'number');

console.log(filteredFriends);

文档您获得无限循环的原因是因为继续语句 IN 。这会跳过该迭代的其余代码,i += 1不会被执行。

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.

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. That skips the rest of the code for that iteration and i += 1 doesn't get executed.

日久见人心 2025-01-25 05:10:18

当您继续时,您不会增加i,因此在下一次迭代中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);
}

When you continue, you do not increment i, so in next iteration i stays the same.

You can always use for ... of to drop need for manually incrementing 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);
}

染火枫林 2025-01-25 05:10:18

您需要每次循环循环时都会增加i,或者您的情况永远无法解决(除非所有人都符合您的状况)。

while (i < friends.length) {
  if (friends[i][0] !== "A" && typeof friends[i] !== "number") {
    console.log(friends[i]);
  }
  i += 1;
}

*我改变了寻找所需结果而不是负面的条件(只是个人喜好)。

You need to increase i every time you loop or your while condition will never resolve (unless all meet your condition).

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).

手心的温暖 2025-01-25 05:10:18

请参阅代码的更改。这将按预期工作。

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++;
}

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++;
}
奢欲 2025-01-25 05:10:18

该代码进入无限循环,因为它不会将I变量递增在IF内部和继续之前。

您的代码可以像这样很容易修复

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;
}

The code enters into an infinite loop because it won´t increment the i variable inside the if and before the continue.

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;
}
找回味觉 2025-01-25 05:10:18

试试这个,希望对你有帮助

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]);
}

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]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文