Khaled Ha
Khaled Ha

Reputation: 41

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"

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

Upvotes: 1

Views: 748

Answers (6)

Boko Moko
Boko Moko

Reputation: 51

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

Upvotes: 0

edlinkiii
edlinkiii

Reputation: 19

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

Upvotes: 1

Mehran Khan
Mehran Khan

Reputation: 1165

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

Upvotes: 0

ahsan
ahsan

Reputation: 1504

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.

Upvotes: 3

TechTrash
TechTrash

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

Upvotes: -3

Justinas
Justinas

Reputation: 43441

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

Upvotes: 1

Related Questions