[ 일일코딩 ] 삼행시 짓기 (Javascript)

원본

http://www.codewars.com/kata/54530f75699b53e558002076/train/javascript

Question

In this kata, we’re going to create the function nato that takes a word and returns a string spells the word using the NATO phonetic alphabet.

There should be a space between each word in the returned string, and the first letter of each word should be capitalized.

For those of you that don’t want your fingers to bleed, this kata already has a dictionary typed out for you.

nato('hi') // --> 'Hotel India'
nato('abc') // --> 'Alpha Bravo Charlie'

My Answer

var nato = (function() {
  var letters =  {
    "A": "Alpha",  "B": "Bravo",   "C": "Charlie",
    "D": "Delta",  "E": "Echo",    "F": "Foxtrot",
    "G": "Golf",   "H": "Hotel",   "I": "India",
    "J": "Juliett","K": "Kilo",    "L": "Lima",
    "M": "Mike",   "N": "November","O": "Oscar",
    "P": "Papa",   "Q": "Quebec",  "R": "Romeo",
    "S": "Sierra", "T": "Tango",   "U": "Uniform",
    "V": "Victor", "W": "Whiskey", "X": "X-ray",
    "Y": "Yankee", "Z": "Zulu"
  }

  return function(word) {
    var natoed = [];
    for(var i=0; i<word.length; i++){
      var letter = word.toUpperCase()[i];
      natoed.push(letters[letter]);
    }
    return natoed.join(" ");
  }
})()

Clever Answer by @ColbyDauph

var nato = (function() {
  var letters =  {
    "A": "Alpha",  "B": "Bravo",   "C": "Charlie",
    "D": "Delta",  "E": "Echo",    "F": "Foxtrot",
    "G": "Golf",   "H": "Hotel",   "I": "India",
    "J": "Juliett","K": "Kilo",    "L": "Lima",
    "M": "Mike",   "N": "November","O": "Oscar",
    "P": "Papa",   "Q": "Quebec",  "R": "Romeo",
    "S": "Sierra", "T": "Tango",   "U": "Uniform",
    "V": "Victor", "W": "Whiskey", "X": "X-ray",
    "Y": "Yankee", "Z": "Zulu"
  }

  return function(word) {
    return word.split('').map(function(v) {
      return letters[v.toUpperCase()]
    }).join(' ');
  }
})()

Published by

Yurim Jin

아름다운 웹과 디자인, 장고와 리액트, 그리고 음악과 맥주를 사랑하는 망고장스터

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s