
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Converting string to MORSE code in JavaScript
What is Morse code?
Morse code is a method used in telecommunications to encode text characters as standardized sequences of two different signal durations, called dots and dashes.
To have a function that converts a particular string to Morse code, we will need an object that maps all the characters (English alphabets) to Morse code equivalents. Once we have that we can simply iterate over the string and construct a new string.
Here is the object that maps alphabets to Morse codes −
Morse Code Map
const morseCode = { "A": ".-", "B": "-...", "C": "-.-.", "D": "-..", "E": ".", "F": "..-.", "G": "--.", "H": "....", "I": "..", "J": ".---", "K": "-.-", "L": ".-..", "M": "--", "N": "-.", "O": "---", "P": ".--.", "Q": "--.-", "R": ".-.", "S": "...", "T": "-", "U": "..-", "W": ".--", "X": "-..-", "Y": "-.--", "Z": "--.." }
Now the function that converts string to Morse code will be −
Example
const morseCode = { "A": ".-", "B": "-...", "C": "-.-.", "D": "-..", "E": ".", "F": "..-.", "G": "--.", "H": "....", "I": "..", "J": ".---", "K": "-.-", "L": ".-..", "M": "--", "N": "-.", "O": "---", "P": ".--.", "Q": "--.-", "R": ".-.", "S": "...", "T": "-", "U": "..-", "W": ".--", "X": "-..-", "Y": "-.--", "Z": "--.." } const convertToMorse = (str) => { return str.toUpperCase().split("").map(el => { return morseCode[el] ? morseCode[el] : el; }).join(""); }; console.log(convertToMorse('Disaster management')); console.log(convertToMorse('hey there!'));
Output
The output in the console will be −
-........-...-..-. --.--..---..--.-.- .....-.-- -......-..!
- Related Articles
- Morse Code Translator in Python
- Unique Morse Code Words in Python
- Converting string to a binary string - JavaScript
- Converting string to an array in JavaScript
- Converting whitespace string to url in JavaScript
- Converting multi-dimensional array to string in JavaScript
- Converting a string to a date in JavaScript
- Converting array to phone number string in JavaScript
- Converting a string to NATO phonetic alphabets in JavaScript
- Converting String to StringBuilder in Java
- Converting ArrayList to String[] in java
- Converting string to date in MongoDB?
- Code to construct an object from a string in JavaScript
- Converting a StringBuilder to String in Java
- Converting list string to dictionary in Python

Advertisements