diff --git a/README.md b/README.md index 0a5d2a51b..8fdb32c4a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ Write your solution in `src/index.js` ## Prepare and test - Install [Node.js](https://nodejs.org/en/) -- Clone this repository: `https://github.com/romacher/morse-decoder.git` +- Fork this repository: `https://github.com/romacher/morse-decoder.git` +- Clone your newly created repo: `https://github.com/<%your_github_username%>/morse-decoder/` - Go to the folder `morse-decoder` - Run `npm install` in command line - Run `npm test` in command line diff --git a/src/index.js b/src/index.js index fbeac9bc6..ea54804b9 100644 --- a/src/index.js +++ b/src/index.js @@ -37,10 +37,22 @@ const MORSE_TABLE = { '-----': '0', }; +const decodeLetter = letter => { + let letter_code = letter.match(/.{1,2}/g).map(v => { + if (v === '10') return '.' + if (v === '11') return '-' + }).join('') + return MORSE_TABLE[letter_code] +} + +const decodeWord = word => { + return word.match(/.{1,10}/g).map(decodeLetter).join('') +} + function decode(expr) { - // write your solution here + return expr.split('**********').map(decodeWord).join(' ') } module.exports = { decode -} \ No newline at end of file +}