cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Untitled

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container">
  <div>
    <h3>Message</h3>
    <input id="input" type="text" placeholder="Input Message"/>
    <button id="send">Convert to Morse Code</button>
  </div>
  <hr />
  <div>
    <h3>Key</h3>
    <br />
    <h4><em>only alphanumeric values and the space character are valid inputs</em></h4>
    <br />
    <h4><em>dot is 1 time unit</em></h4>
    <h4><em>dash is 3 time units</em></h4>
    <h4><em>word space is 7 time units</em></h4>
    <br />
    <h4><em>same letter space ('' empty string) is 1 time unit</em></h4>
    <h4><em>same word space (+ plus sign) is 3 time units</em></h4>
  </div>
  <hr />
  <div>
    <h3>Morse Code</h3>
    <p id="output"><em>None</em></p>
  </div>
</div>
            
          
!
            
              body {
  background-color: #ccc;
}

.container {
  margin: 15px;
  padding: 15px;
}

h3, h4 {
  margin: 0;
  text-align: center;
  font-size: 18pt;
}

h4 {
  font-size: 11pt;
}

#output {
  margin-top: 15px;
  font-size: 14pt;
}

input, button {
  text-align: center;
  padding: 10px;
  margin: 20px 0 0;
  box-sizing: border-box;
  position: relative;
  width: 100%;
}

button {
  cursor: pointer;
  margin-top: 7px;
}

hr {
  margin: 30px;
}
            
          
!
            
              'use strict';

//cache
var $input = document.getElementById('input'),
    $output = document.getElementById('output'),
    $btnSend = document.getElementById('send'),
    speedMultiplier = 100,
    dot = '.', dash = '-', spaceLetter = '+', spaceWord = ' ', invalid = '?',
    soundDot = new Howl({urls: ['https://a.clyp.it/kysuohe2.mp3']}), //ideally short
    soundDash = new Howl({urls: ['https://a.clyp.it/kysuohe2.mp3']}), //ideally extended short sample vs the same sample
    
    //via https://en.wikipedia.org/wiki/Morse_code#/media/File:International_Morse_Code.svg
    charCodesAndTimeUnits = {
      '.': 1, //dot
      '-': 3, //dash
      ' ': 7, //word new
      '+': 3, //letter new
      '': 1, //letter same
    },
    morseCode = [
      { "character": " ", "morsecode": " "},
      { "character": "A", "morsecode": ".-"},
      { "character": "B", "morsecode": "-..."},
      { "character": "C", "morsecode": "-.-."},
      { "character": "D", "morsecode": "-.."},
      { "character": "E", "morsecode": "."},
      { "character": "F", "morsecode": "..-."},
      { "character": "G", "morsecode": "--."},
      { "character": "H", "morsecode": "...."},
      { "character": "I", "morsecode": ".."},
      { "character": "J", "morsecode": ".---"},
      { "character": "K", "morsecode": "-.-"},
      { "character": "L", "morsecode": ".-.."},
      { "character": "M", "morsecode": "--"},
      { "character": "N", "morsecode": "-."},
      { "character": "O", "morsecode": "---"},
      { "character": "P", "morsecode": ".--."},
      { "character": "Q", "morsecode": "--.-"},
      { "character": "R", "morsecode": ".-."},
      { "character": "S", "morsecode": "..."},
      { "character": "T", "morsecode": "-"},
      { "character": "U", "morsecode": "..-"},
      { "character": "V", "morsecode": "...-"},
      { "character": "W", "morsecode": ".--"},
      { "character": "X", "morsecode": "-..-"},
      { "character": "Y", "morsecode": "-.--"},
      { "character": "Z", "morsecode": "--.."},
      
      { "character": "1", "morsecode": ".----"},
      { "character": "2", "morsecode": "..---"},
      { "character": "3", "morsecode": "...--"},
      { "character": "4", "morsecode": "....-"},
      { "character": "5", "morsecode": "....."},
      { "character": "6", "morsecode": "-...."},
      { "character": "7", "morsecode": "--..."},
      { "character": "8", "morsecode": "---.."},
      { "character": "9", "morsecode": "----."},
      { "character": "0", "morsecode": "-----"},
  ];

//init
$btnSend.addEventListener('click', onSend);

//methods
function parseMessage() {
  
  //cache
  var inputArr = $input.value.toUpperCase().split(''),
      appender,
      result = "";

  //convert
  for(var character of inputArr) {
    appender = character === spaceWord ? '' : spaceLetter;
    result += parseCharacter(character) + appender;
  }
  
  //clean append  
  if(result.endsWith(spaceLetter)) {
    result = result.slice(0, result.length - 1);
  }
  
  //result
  return result;
}

function parseCharacter(character) {
  var result;
  
  //match
  for(var i = 0, len = morseCode.length; i < len; i++) {
    if(morseCode[i].character === character) {
      result = morseCode[i].morsecode;
      break;
    }
  }
  
  //invalid
  if(!result) {
    result = invalid;
  }
  
  return result;
}

function playMessage(message) {
  
  //pattern display
  $output.innerHTML = message + '<br/>';
 
  //playback
  for(let i = 0, delay = 500, key, len = message.length; i < len; i++) {

    //play tone
    key = message.charAt(i);
    setTimeout(function() { playTone(key); }, delay);
    
    //delay update
    delay += calcDelay(key);
  }
}

function playTone(key)  {
  $output.innerHTML += key;
  if(key === dot) {
    soundDot.play();
  }  else if (key === dash) {
    soundDash.play();
  }
}

function calcDelay(key) {
    return speedMultiplier * charCodesAndTimeUnits[key];
}

//handlers
function onSend() {
  playMessage(parseMessage());
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console