JSFiddle - React, Tailwind, and code Playground

by Andrew Maxwell

JavaScript

console.clear();

const morseMap = {
	0: '-----',
  1: '.----',
  2: '..---',
  3: '...--',
  4: '....-',
  5: '.....',
  6: '-....',
  7: '--...',
  8: '---..',
  9: '----.',
  ' ': '/'
};

const spellingMap = {
	'-': 'twinkie',
  '.': 'cupcake',
  ' ': '',
  '/': 'slash'
};

const pad = str => ('00000000' + str).substring(str.length);

const toMorse = number => number.toString().split('').map(c => morseMap[c]).join(' ');
const spellItOut = str => str.split('').map(c => spellingMap[c]).join('-').replace(/--/g, ' ');
const toBinary = str => str.split('').map(c => pad(c.charCodeAt(0).toString(2))).join('');

function translate(number){
	var inMorse = toMorse(number);
  var spelledOut = spellItOut(inMorse);
  var inBinary = toBinary(spelledOut);
  
  console.log('Morse:', inMorse);
  console.log('Spelled out:', spelledOut);
  console.log('In Binary:', inBinary);
  console.log('Number: ', parseInt(inBinary, 2));
}

translate(99);