Everyone Can Code!

JavaScript Kata #12: Friend or Foe?

By YayoDrayber | Learn To Code With Me | 16 Aug 2022


If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.

If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.

Kata #12

DESCRIPTION:

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.

Starting code:

function friend(friends){
  //your code here
}

My attempt that worked:

function friend(friends){
  //your code here
  let arrHolder = []
  let strHolder = ""
  for (i = 0 ; i < friends.length ; i++){
    strHolder = friends[i].toString()
    if(strHolder.length == 4){
      arrHolder.push(strHolder)
    } else{}
      }
  return arrHolder
}

Rank #1 'Best Practice':

function friend(friends){
  return friends.filter(n => n.length === 4)
}

Ranks #2, #4 and #5, #7 through #9 also made use of the .filter method.

Following is rank #3 (submitted by user FepAguilar) which is similar to #6 (submitted by users Paragon and  rnegrelly), only difference being in variable declaration:

function friend(friends){
  //Create new array called "myFriends" for add your friends
  var i,
      len = friends.length,
      myFriends = [];  
  for (i = 0; i < len; i++) {
       //Check for names with length equal to four and it is not a number
       if(friends[i].length == 4 && isNaN(friends[i])) {
          myFriends.push(friends[i]);
       }
  }
  return myFriends;
}

I guess I am lumped in with them and together, the four of us are far behind the pack not knowing about the .filter method at the time. 🤣

Oh well, now we know. ¯\_(ツ)_/¯ Gotta keep on learning!

That's it for Kata #12, stay tuned for more katas to be solved!

Link to Kata #1: Square(n) Sum

Link to Kata #2: Convert a Number to a String

Link to Kata #3: DNA to RNA Conversion

Link to Kata #4: Remove First and Last Character

Link to Kata #5: MakeUpperCase

Link to Kata #6: Total amount of points

Link to Kata #7: A Needle in the Haystack

Link to Kata #8: Sum of positive

Link to Kata #9: Basic Mathematical Operations

Link to Kata #10: Beginner - Reduce but Grow

Link to Kata #11: Square Every Digit

 

 

 

 

How do you rate this article?

2


YayoDrayber
YayoDrayber

A Yayo, A Drayber, among others.


Learn To Code With Me
Learn To Code With Me

Just started with Codewars and will be posting the solutions I came up with here and the apparent best practices.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.