DEV Community

Cover image for Day 03: 30 Days of Codewars.js
Youssef Rabei
Youssef Rabei

Posted on

Day 03: 30 Days of Codewars.js

Table Of Contents


Testing 1-2-3 : ✍ by acr

📃 Description

Write a function which takes a list of strings and returns each line prepended by the correct number.

Examples:

number([]) // => []
number(["a", "b", "c"]) // => ["1: a", "2: b", "3: c"]
Enter fullscreen mode Exit fullscreen mode

🤔 Thinking

map over the array and return an array using template literal every element with its index

👨‍💻 Code

const number = arr => arr.map((elm, i) => `${i+1}: ${elm}`)
Enter fullscreen mode Exit fullscreen mode

Stop gninnipS My sdroW! : ✍ by xDranik

📃 Description

Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.

Examples:

spinWords( "Hey fellow warriors" ) // => "Hey wollef sroirraw"
spinWords( "This is a test") // =>"This is a test"
spinWords( "This is another test" ) // =>"This is rehtona test"
Enter fullscreen mode Exit fullscreen mode

🤔 Thinking

Turn the string into an array of words (strings)
Then Check if the word length >= 5
If true I will reverse the word(s)
Then join it all

👨‍💻 Code

const spinWords = str => {
  const arr = str.split(" ");
  const spinedarr = arr.map(word => {
   return word.length >= 5 ? word.split("").reverse().join("") : word;
  })

  return spinedarr.join(" ");
}
Enter fullscreen mode Exit fullscreen mode

Credit Card Mask : ✍ by samranjbari

📃 Description

Write a function maskify, which changes all but the last four characters into '#'.

Examples:

maskify("4556364607935616") // => "############5616"
maskify(     "64607935616") // =>      "#######5616"
maskify(               "1") // =>               "1"
Enter fullscreen mode Exit fullscreen mode

🤔 Thinking

I'm will make an array and then slice it so I get the last 4 digits
Then use the padStart method to create a string with the same length with that starts with #s

🤷‍♂️ What?

MDN:

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

👨‍💻 Code

const maskify = cc => {
  const last4 = cc.split("").slice(-4);

  const masked = cc.length >= 4 ? last4.join("").padStart(cc.length, "#") : cc;

  return masked
}
Enter fullscreen mode Exit fullscreen mode

If you know a better way to solve any of the previous katas let me know in the comment

Thanks for reading, I really appreciate it.

Top comments (1)

Collapse
 
criscmd profile image
Tao Christopher Takahashi

You're doing great!!!
Just one note tho.
When creating a function or a method that takes in param/props the name should be more clear and specific for better readability. when you have time you should check The Art Of Readable Code. It will 1up your game for sure!