Error in passing test case for echo implementing quoting:single quotes

if (command === “echo”) {
const input = answer;
const regex = /^echo\s+(.+)$/; // Match everything after “echo”
const match = input.match(regex);

  if (!match) {
    console.error("Invalid syntax for echo");
    return;
  }

  const parts = match[1].match(/'([^']*)'|\S+/g); // Match quoted parts or standalone words

  let output = "";
  let lastWasQuoted = false;
  console.log(parts)

  parts.forEach((part, index) => {
    if (part.startsWith("'") && part.endsWith("'")) {
      let content = part.slice(1, -1); // Remove surrounding quotes
      console.log(content)
      if (lastWasQuoted) {
        output += content;
        console.log(output) // Concatenate directly if last was quoted
      } else {
        output += (output ? " " : "") + content;
        console.log(output) // Add space if needed
      }
      lastWasQuoted = true;
    } else {
      output += (output ? " " : "") + part; // Always add space before unquoted words
      console.log(output)
      lastWasQuoted = false;
    }
  });

  console.log(output);

}

Hi @sassius, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

@sassius Looks like you’ve got past this stage. Do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.