Home > Back-end >  javascript how can remove indexes inside array thats starts with certain letter
javascript how can remove indexes inside array thats starts with certain letter

Time:11-09

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

while (index < friends.length) {
  index  ;
  if (
    typeof friends[index] === "number" &&
    friends[index] == friends[index].startsWith("a".toUpperCase())
  ) {
    continue;
  }
  console.log(friends[index]);
}

what's wrong with my code i get a syntax error this is the output i want i want to remove the numbers and the names thats starts with letter a

Output "1 => Sayed "2 => Mahmoud

CodePudding user response:

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

while (index < friends.length) {
  if (typeof friends[index] === "number" || friends[index].startsWith("A")) {
    index  ;
    continue;
  } else {
    console.log(`${  counter} => ${friends[index]}`);
    index  ;
  }
}

i found the solotion

CodePudding user response:

A few problems:

First, as mentioned, index first in the loop block increments index to 1. So the loop skips the first element and starts with the second element, and tries to access a non-existent element on the final loop.

// the array:
["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"]

// the array indexes:
[0, 1, 2, 3, 4, 5, 6]

// the indexes called by the loop:
[1, 2, 3, 4, 5, 6, 7]

Second,

typeof friends[index] === "number" && friends[index] == friends[index].startsWith("a".toUpperCase())

...is an impossibility—always false. Do you mean:

typeof index === "number" && friends[index] == friends[index].startsWith("a".toUpperCase())

...and if so, why? If not, what? (EDIT: Question answered by question update—some elements are numbers.)

Furthermore, how is:

friends[index] == friends[index].startsWith("a".toUpperCase())

...a possiblity? startsWith() returns boolean true or false. "soft" equality == will equate negative nubmers and 0 to false, and any other number to true.

Third, there's nothing removing elements from the array.

Fourth, if the code did accomplish to remove elements from the friends array it would alter the index'ed location of the array elements, and the sequence flow would be corrupted.

Resolving these problems, and push()'ing to a new array:

let friends = ["Ahmed", "Sayed", "Ali", 1, 2, "Mahmoud", "Amany"];
let friends_noa = [];
let index = 0;
let counter = 0;

while (index < friends.length) {
  if (
    typeof friends[index] === "number" ||
    friends[index].startsWith("a".toUpperCase())
  ) {
    index  ;
    continue;
  }
  friends_noa.push(friends[index]);
  console.log(friends[index]);
  index  ;
}
console.log(friends_noa);

Also, if your challenge does not allow you to create a new array, use slice():

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

while (index < friends.length) {
  if (
    typeof friends[index] === "number" ||
    friends[index].startsWith("a".toUpperCase())
  ) {
    friends.splice(index, 1);
    continue;
  }
  console.log(friends[index]);
  index  ;
}
console.log(friends);

However, it's simpler to use filter()—this is what it's made for:

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

friends = friends.filter(function (name) {
  return (("number" !== typeof name) 
    && !name.startsWith("A"));
});

console.log(friends);

  • Related