<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" src="/images/favicon.gif" />
<title>Spaceless Morse Code Solver</title>
<style type="text/css"></style>
<script type="text/javascript">
var morse = {
".": "E", "-": "T", "..": "I", ".-": "A", "-.": "N", "--": "M", "...": "S",
"..-": "U", ".-.": "R", ".--": "W", "-..": "D", "-.-": "K", "--.": "G",
"---": "O", "....": "H", "...-": "V", "..-.": "F", ".-..": "L", ".--.": "P",
".---": "J", "-...": "B", "-..-": "X", "-.-.": "C", "-.--": "Y",
"--..": "Z", "--.-": "Q", "----.": "9", "---..": "8", "--...": "7",
"-....": "6", ".....": "5", "....-": "4", "...--": "3", "..---": "2",
".----": "1", "-----": "0"
};
const maxLen = 5;
function tryMatch (decoded, remains, n) {
var [first, left] = [remains.substring(0, n), remains.substring(n)];
if (morse[first]) {
decoded += morse[first];
if (!left.length) return document.write("candidate solution: "+decoded+"<br />");
for (var i = maxLen; i && tryMatch(decoded, left, i); i--);
}
return true;
}
var textarea;
function parse () {
for (var i = maxLen; i; i--) tryMatch('', textarea.value, i);
}
window.onload = function () {
textarea = document.querySelector("textarea");
document.querySelector("button").onclick = parse;
};
</script>
</head>
<body>
<textarea>-...--..-..-.--</textarea>
<button>hax</button>
</body>
</html>