User:Kiken/vector.js

From Esolang
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// js interpreter for morse code (esolang)
try{
function morseCode(code) {
  let yield_ = code;
  let index = 0;

  let morsetochar = {
    ".-": "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",
    ".----": "1",
    "..---": "2",
    "...--": "3",
    "....-": "4",
    ".....": "5",
    "-....": "6",
    "--...": "7",
    "---..": "8",
    "----.": "9",
    "-----": "0",
    ".-.-.-": ".",
    "--..--": ",",
    "..--..": "?",
    "": ""
  };

  while (yield_) {
    if (index + 1 <= code.length) {
      if (code[index] == ".") {
        if (code[index + 1] == ".") {
          yield_ =
            yield_.slice(1, yield_.indexOf("-")) +
            "." +
            yield_.slice(yield_.indexOf("-"));
        } else if (code[index + 1] == "-") {
          yield_ =
            yield_.slice(1, yield_.indexOf(".")) +
            "-" +
            yield_.slice(yield_.indexOf("."));
        } else {
          yield_ = yield_.slice(1);
        }
        continue;
      } else if (code[index] == "-") {
        if (code[index + 1] == ".") {
          yield_ = yield_.charAt(1) + "-" + yield_.slice(1);
        } else if (code[index + 1] == "-") {
          yield_ = yield_.charAt(0) + yield_.slice(2);
          yield_ = yield_.slice(0, 3) + yield_.charAt(0) + yield_.slice(3);
        } else {
          yield_ = yield_.slice(1) + yield_;
        }
        continue;
      } else {
        break;
      }
    } else {
      break;
    }
    index += 1;
  }

  let englishPlainText = "";
  let morseCode = yield_;

  let currentCharMorseCode = "";
  let i = 0;

  while (i < morseCode.length - 1) {
    if (morseCode[i] == " ") {
      if (currentCharMorseCode.length == 0 && morseCode[i + 1] == " ") {
        englishPlainText += " ";
        i += 1;
      } else {
        englishPlainText += morsetochar[currentCharMorseCode];
        currentCharMorseCode = "";
      }
    } else {
      currentCharMorseCode += morseCode[i];
    }

    i += 1;
  }

  if (currentCharMorseCode.length > 0) {
    try {
      englishPlainText += morsetochar[currentCharMorseCode];
    } catch (err) {
      englishPlainText += "#";
    }
  }

  return englishPlainText;
}

var inp = document.createElement("input");
var btn = document.createElement("button");
var out = document.createElement("p");
var div = doument.createElement("div");

div.style.position = "fixed";

inp.style.top = "100px";
btn.style.top = "100px";
out.style.top = "100px";

btn.innerText = "submit";
out.innerText = "output goes here";
inp.placeholder = "input goes here";

inp.style.left = "100px";
btn.style.left = "100px";
out.style.left = "100px";

out.id = "out";

btn.onclick = function () {document.getElementById("out").innerText = morseCode(inp.value);};

document.body.appendChild(div);
div.appendChild(inp);
div.appendChild(btn);
div.appendChild(out);
}catch(_0xe499b9){alert(_0xe499b9.message)}