Friend or Foe?

Lee·2022년 7월 30일

Algorithm

목록 보기
61/92
post-thumbnail

❓Friend or Foe?

Q. Make a program that filters a list of strings and returns a list with only your friends name in it.

If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not...

Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]

i.e.

friend ["Ryan", "Kieran", "Mark"] shouldBe ["Ryan", "Mark"]
Note: keep the original order of the names in the output.

✔ Solution

//#my solution
function friend(friends) {
  //your code here
  return friends.filter((item) => item.length == 4);
}

//other solution
function friend(friends){
  //your code here
  var realFriends = [];
  for(i=0; i<friends.length; i++){
    if(friends[i].length == 4 && isNaN(friends[i])){
      realFriends.push(friends[i]);
    }
  }
  
  return realFriends
}
profile
Lee

0개의 댓글