Algorithm/codewars

Decode the Morse code 답안

Bonita SY 2021. 3. 23. 18:04
728x90
반응형

문제)

www.codewars.com/kata/54b724efac3d5402db00065e/train/javascript

 

Codewars: Achieve mastery through challenge

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

내가 푼 답)

const Morse_code = {
  '...---...': 'SOS',
  '.-' : 'A',
  '-...':'B',
  '-.-.': 'C',
  '-..': 'D',
  '.': 'E',
  '..-.': 'F',
  '--.': 'G',
  '....' : 'H',
  '..': 'I',
  '.---': 'J',
  '-.-': 'K',
  '.-..':'L',
  '--': 'M',
  '-.': 'N',
  '---': 'O',
  '.--.': 'P',
  '--.-': 'Q',
  '.-.': 'R',
  '...': 'S',
  '-': 'T',
  '..-': 'U',
  '...-': 'V',
  '.--': 'W',
  '-..-': 'X',
  '-.--': 'Y',
  '--..':'Z',
  '.-.-.-': '.',
  '--..--': ',',
  '-..-.': '\/',
  '-.-.--': '!'
};

decodeMorse = function(morseCode){  
  var result = [];
  morseCode.split('   ').forEach(word => {
    var toAsciiWord = '';
    word.split(' ').forEach(w => {
      if (Morse_code[w]) {
        toAsciiWord += Morse_code[w] ;  
      }
    }, this)
    result.push(toAsciiWord);
  }, this)
  
  return result.join(' ').trim();
}

 

테스트 결과)

 

마음에 드는 다른 사람 소스)

728x90
반응형

'Algorithm > codewars' 카테고리의 다른 글

You're a square! 답안  (0) 2021.03.23
Descending Order 답안  (0) 2021.03.23
Convert string to camel case 답안  (0) 2021.03.23
Categorize New Member 답안  (0) 2021.03.23
Jaden Casing Strings 답안  (0) 2021.03.22