DEV Community

Cesar Del rio
Cesar Del rio

Posted on

#43 - If you can read this... - Codewars Kata (6 kyu)

Instructions

Task
You'll have to translate a string to Pilot's alphabet (NATO phonetic alphabet).

Note:

There are preloaded dictionary you can use, named NATO
The set of used punctuation is ,.!?.
Punctuation should be kept in your return string, but spaces should not.
Xray should not have a dash within.
Every word and punctuation mark should be seperated by a space ' '.
There should be no trailing whitespace

Example

Input:

If, you can read?

Output:

India Foxtrot , Yankee Oscar Uniform Charlie Alfa November Romeo Echo Alfa Delta ?


My solution:

function to_nato(words) {
  let arr = words.split('').filter(x=> x!== ' ')
  let r = ''
  arr.map((x,i)=>{
    let letter = x.toLowerCase()
    let coded = NATO[letter]
    if(i !== arr.length - 1 && coded) r+=coded + ' '
    else if(i == arr.length- 1 && coded) r += coded
    else if(i !== arr.length - 1 && !coded) r += letter + ' '
    else r+=letter
  })
  return r
}
Enter fullscreen mode Exit fullscreen mode

Explanation

First I created a variable with the string splitted into an array, and another variable "r" with an empty string, where I'll save the last result.

  let arr = words.split('').filter(x=> x!== ' ')
  let r = ''
Enter fullscreen mode Exit fullscreen mode

Then I mapped that array with 2 parameters, x: the letter being mapped and i: the index of it.
First in the loop I converted to lower case the letter and after that I made the variable "coded" to save the letter in pilots alphabet with the NATO object.

  arr.map((x,i)=>{
    let letter = x.toLowerCase()
    let coded = NATO[letter]
Enter fullscreen mode Exit fullscreen mode

Then I used 4 conditionals:
1- If the index of the letter isn't equal to the array length - 1, it means that it isn't the last one, and coded isn't undefined it means it exist, so I'll add to the result string the coded plus a space

    if(i !== arr.length - 1 && coded) r+=coded + ' '
    else r+=letter
Enter fullscreen mode Exit fullscreen mode

2- Else if the letter is the last one in the array and coded exist I'll just add coded to the result without the sapce because is the last letter

    else if(i == arr.length- 1 && coded) r += coded
Enter fullscreen mode Exit fullscreen mode

3- Else if it isn't the last letter and there's no coded string it means that it is one of the symbols like .?!, so I just add the letter with an space

    else if(i !== arr.length - 1 && !coded) r += letter + ' '
    else r+=letter
Enter fullscreen mode Exit fullscreen mode

4- If any condition is true it means that it is a symbol and it's the last element of the array, so It doesn't need a space, so I just add the letter

    else r += letter
Enter fullscreen mode Exit fullscreen mode

At the end I just returned r

    return r
Enter fullscreen mode Exit fullscreen mode

What do you think about this solution? 👇🤔

My Github
My twitter
Solve this Kata

Top comments (0)