Skip to content

Commit f003b79

Browse files
Merge pull request #252 from developer-yash03/feature/morse-to-text
Added Feature Morse to Text
2 parents 31eb0a2 + 2415558 commit f003b79

2 files changed

Lines changed: 114 additions & 21 deletions

File tree

web-app/js/projects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Project Registry
1+
// Project Registry
22
// Each project's HTML and logic lives in its own file under js/projects/
33

44
function getProjectHTML(projectName) {

web-app/js/projects/morse-code.js

Lines changed: 113 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ function getMorseCodeHTML() {
44
<h2>📻 Morse Code Translator</h2>
55
<div class="morse-container">
66
<div class="input-section">
7-
<label for="textInput">Enter Text:</label>
7+
<div class="mode-toggle">
8+
<input type="radio" id="modeTextToMorse" name="translationMode" value="textToMorse" checked>
9+
<label for="modeTextToMorse">Text to Morse</label>
10+
<input type="radio" id="modeMorseToText" name="translationMode" value="morseToText">
11+
<label for="modeMorseToText">Morse to Text</label>
12+
</div>
13+
<label id="inputLabel" for="textInput">Enter Text:</label>
814
<textarea id="textInput" rows="4" placeholder="Type your message here..."></textarea>
915
<button class="btn-translate" id="translateBtn">📻 Translate to Morse</button>
1016
</div>
1117
1218
<div class="output-section">
13-
<h3>Morse Code Output:</h3>
19+
<h3>Translation Output:</h3>
1420
<div class="morse-output" id="morseOutput">
15-
<p class="placeholder">Your morse code will appear here...</p>
21+
<p class="placeholder">Your translation will appear here...</p>
1622
</div>
1723
</div>
1824
@@ -34,6 +40,33 @@ function getMorseCodeHTML() {
3440
margin-bottom: 2rem;
3541
}
3642
43+
.mode-toggle {
44+
display: flex;
45+
gap: 1rem;
46+
margin-bottom: 1.5rem;
47+
}
48+
49+
.mode-toggle input[type="radio"] {
50+
display: none;
51+
}
52+
53+
.mode-toggle label {
54+
padding: 0.5rem 1rem;
55+
border: 2px solid var(--primary-color);
56+
border-radius: 8px;
57+
cursor: pointer;
58+
font-weight: bold;
59+
color: var(--text-color);
60+
transition: var(--transition);
61+
margin-bottom: 0 !important;
62+
display: inline-block !important;
63+
}
64+
65+
.mode-toggle input[type="radio"]:checked + label {
66+
background: var(--primary-color);
67+
color: white;
68+
}
69+
3770
.input-section label {
3871
display: block;
3972
margin-bottom: 0.5rem;
@@ -149,13 +182,39 @@ function initMorseCode() {
149182
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
150183
'Y': '-.--', 'Z': '--..', '0': '-----', '1': '.----', '2': '..---',
151184
'3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
152-
'8': '---..', '9': '----.', ' ': '/'
185+
'8': '---..', '9': '----.', '.': '.-.-.-', ',': '--..--', '?': '..--..',
186+
'!': '-.-.--', '-': '-....-', '/': '-..-.', '@': '.--.-.', '(': '-.--.',
187+
')': '-.--.-', ' ': '/'
153188
};
154189

155190
const textInput = document.getElementById('textInput');
156191
const translateBtn = document.getElementById('translateBtn');
157192
const morseOutput = document.getElementById('morseOutput');
158193
const morseChart = document.getElementById('morseChart');
194+
const modeTextToMorse = document.getElementById('modeTextToMorse');
195+
const modeMorseToText = document.getElementById('modeMorseToText');
196+
const inputLabel = document.getElementById('inputLabel');
197+
198+
// Switch Modes
199+
modeTextToMorse.addEventListener('change', () => {
200+
if (modeTextToMorse.checked) {
201+
inputLabel.textContent = 'Enter Text:';
202+
textInput.placeholder = 'Type your message here...';
203+
textInput.value = '';
204+
morseOutput.innerHTML = '<p class="placeholder">Your translation will appear here...</p>';
205+
translateBtn.innerHTML = '📻 Translate to Morse';
206+
}
207+
});
208+
209+
modeMorseToText.addEventListener('change', () => {
210+
if (modeMorseToText.checked) {
211+
inputLabel.textContent = 'Enter Morse Code:';
212+
textInput.placeholder = "Type morse code (separate letters with space, words with double spaces)...";
213+
textInput.value = '';
214+
morseOutput.innerHTML = '<p class="placeholder">Your translation will appear here...</p>';
215+
translateBtn.innerHTML = '📝 Translate to Text';
216+
}
217+
});
159218

160219
Object.keys(morseCode).forEach(char => {
161220
if (char !== ' ') {
@@ -172,29 +231,63 @@ function initMorseCode() {
172231
function translate() {
173232
const text = textInput.value.toUpperCase();
174233
if (!text.trim()) {
175-
morseOutput.innerHTML = '<p class="placeholder">Please enter some text to translate!</p>';
234+
morseOutput.innerHTML = '<p class="placeholder">Please enter something to translate!</p>';
176235
return;
177236
}
178237

179238
morseOutput.innerHTML = '';
180-
const words = text.split(' ');
181239

182-
words.forEach((word, wordIndex) => {
183-
let morseWord = '';
184-
for (let char of word) {
185-
if (morseCode[char]) {
186-
morseWord += morseCode[char] + ' ';
240+
if (modeMorseToText.checked) {
241+
// Morse to Text
242+
const reverseMorseCode = {};
243+
Object.keys(morseCode).forEach(key => {
244+
reverseMorseCode[morseCode[key]] = key;
245+
});
246+
247+
const words = text.trim().split(/(?:\s*\/\s*|\s{2,})/);
248+
249+
words.forEach((word, wordIndex) => {
250+
const chars = word.trim().split(/\s+/);
251+
let textWord = '';
252+
chars.forEach(char => {
253+
if (reverseMorseCode[char]) {
254+
textWord += reverseMorseCode[char];
255+
} else if (char) {
256+
textWord += '?';
257+
}
258+
});
259+
260+
if (textWord.trim()) {
261+
const wordEl = document.createElement('div');
262+
wordEl.className = 'morse-word';
263+
wordEl.style.fontFamily = 'inherit';
264+
wordEl.style.letterSpacing = '2px';
265+
wordEl.textContent = textWord.trim();
266+
wordEl.style.animationDelay = `${wordIndex * 0.1}s`;
267+
morseOutput.appendChild(wordEl);
187268
}
188-
}
269+
});
270+
} else {
271+
// Text to Morse
272+
const words = text.split(' ');
189273

190-
if (morseWord.trim()) {
191-
const wordEl = document.createElement('div');
192-
wordEl.className = 'morse-word';
193-
wordEl.textContent = morseWord.trim();
194-
wordEl.style.animationDelay = `${wordIndex * 0.1}s`;
195-
morseOutput.appendChild(wordEl);
196-
}
197-
});
274+
words.forEach((word, wordIndex) => {
275+
let morseWord = '';
276+
for (let char of word) {
277+
if (morseCode[char]) {
278+
morseWord += morseCode[char] + ' ';
279+
}
280+
}
281+
282+
if (morseWord.trim()) {
283+
const wordEl = document.createElement('div');
284+
wordEl.className = 'morse-word';
285+
wordEl.textContent = morseWord.trim();
286+
wordEl.style.animationDelay = `${wordIndex * 0.1}s`;
287+
morseOutput.appendChild(wordEl);
288+
}
289+
});
290+
}
198291
}
199292

200293
translateBtn.addEventListener('click', translate);

0 commit comments

Comments
 (0)