Certainly! Below is an example of a simple website that translates Morse code to English text. The code is contained in a single HTML file, using modern HTML5, CSS3, and JavaScript practices.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Morse Code Translator</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
background-color: #f4f4f4;
color: #333;
}
h1 {
text-align: center;
}
.container {
max-width: 600px;
margin: auto;
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
font-size: 16px;
}
button {
padding: 10px 20px;
font-size: 16px;
margin-top: 10px;
cursor: pointer;
}
#translation {
margin-top: 20px;
padding: 10px;
background-color: #fff;
min-height: 50px;
border: 1px solid #ccc;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container">
<h1>Morse Code Translator</h1>
<p>Enter Morse code below (use spaces between letters and "/" between words):</p>
<textarea id="morseInput" placeholder="e.g., .... . .-.. .-.. --- / .-- --- .-. .-.. -.."></textarea>
<button id="translateBtn">Translate</button>
<div id="translation"></div>
</div>
<script>
(() => {
const morseCodeMap = {
'.-': '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', '-----': '0', '.----': '1', '..---': '2',
'...--': '3', '....-': '4', '.....': '5', '-....': '6',
'--...': '7', '---..': '8', '----.': '9', '.-.-.-': '.',
'--..--': ',', '..--..': '?', '-.-.--': '!', '-....-': '-',
'-..-.': '/', '.--.-.': '@', '-.--.': '(', '-.--.-': ')',
'/': ' '
};
const translateBtn = document.getElementById('translateBtn');
const morseInput = document.getElementById('morseInput');
const translationDiv = document.getElementById('translation');
const translateMorseToText = (morseCode) => {
return morseCode
.trim()
.split(' ')
.map(code => morseCodeMap[code] || '')
.join('');
};
translateBtn.addEventListener('click', () => {
const morseCode = morseInput.value;
const translation = translateMorseToText(morseCode);
translationDiv.textContent = translation || 'Translation not available.';
});
})();
</script>
</body>
</html>
Instructions:
- Save the code above into a file with an
.html extension, for example, morse_translator.html.
- Open the file in a modern web browser (like Chrome, Firefox, or Edge).
How it works:
- The page includes a textarea where you can input Morse code. Use spaces to separate letters and
/ to separate words.
- Example Morse code:
.... . .-.. .-.. --- / .-- --- .-. .-.. -.. translates to HELLO WORLD.
- When you click the "Translate" button, the JavaScript function
translateMorseToText is called.
- The Morse code input is split into individual codes, each corresponding to a letter or symbol.
- Each Morse code symbol is mapped to its corresponding English character using the
morseCodeMap object.
- The translated text is displayed below the button.
Features and Modern Patterns:
- Immediately Invoked Function Expression (IIFE): The JavaScript code is wrapped in an IIFE to avoid polluting the global namespace.
const and let: Modern variable declarations are used for block scoping and to prevent reassignment where appropriate.
- Arrow Functions: The
translateMorseToText function uses ES6 arrow function syntax for brevity and clarity.
- Event Listeners: Modern
addEventListener method is used to handle the button click event.
- Responsive Design: The
meta viewport tag ensures the page is mobile-friendly.
- Semantic HTML5 Elements: Elements like
<header>, <footer>, etc., are used where appropriate (though not in this simple example).
- CSS3 Styles: Modern CSS is used for styling, including resetting margins, setting fonts, and responsive dimensions.
Feel free to customize the styles and enhance the functionality as needed!