Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Split String On Single Space and replace three spaces with a single space

I am struggling to pass the below test, passing in the following following MorseCode to my code, "--. --- --- -.. -- --- .-. -. .. -. --. -. --- .-. - .... -.-. --- -.. . .-. ..." Any ideas on how I can handle " " 3 spaces without affecting the translation? Expected: "GOOD MORNING NORTHCODERS" Received: "GOODMORNINGNORTHCODERS"

function morseCode(str) {
  if (str === undefined) {
    return "";
  }

  const morseDic = {
    ".-": "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 outArr = [];
  const myArr = str.split(' ');
  myArr.forEach((el) => outArr.push(morseDic[el]));
  let newString = outArr.toString();
  newString = newString.replaceAll(",", '');
  return newString;
};

const result = morseCode("--. --- --- -..   -- --- .-. -. .. -. --.   -. --- .-. - .... -.-. --- -.. . .-. ...");

console.log(result);

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!