# Struggling to understand what's causing this error

**URL:** <https://forum.codecrafters.io/t/struggling-to-understand-whats-causing-this-error/6015>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [March 4, 2025, 5:21pm UTC](https://forum.codecrafters.io/t/struggling-to-understand-whats-causing-this-error/6015 "2025-03-04T17:21:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas-EG](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/nicholas-eg/32/5194_2.png) [@Nicholas-EG](https://forum.codecrafters.io/u/Nicholas-EG)\
**Post date:** [March 4, 2025, 5:21pm UTC](https://forum.codecrafters.io/t/struggling-to-understand-whats-causing-this-error/6015/1 "2025-03-04T17:21:49Z")

</div>

I’m stuck on Stage #NI6. The problem appears to be that my code is adding a newline character before returning the prompt, however I can’t find where this character is being added.

I’ve tried testing locally on my machine, however there doesn’t appear to be a newline character when I run the cat command with multiple files. I also played with how the cat output is returned to the terminal, but that didn’t make a difference. I would appreciate if someone could look over my code and point me in the right direction.

Here are my logs:

```auto
remote: [tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
remote: [tester::#NI6] [setup] export PATH=/tmp/grape/mango/grape:$PATH
remote: [tester::#NI6] Running ./your_program.sh
remote: [tester::#NI6] [setup] echo -n "mango blueberry." > "/tmp/quz/f 51"
remote: [tester::#NI6] [setup] echo -n "pineapple strawberry." > "/tmp/quz/f 12"
remote: [tester::#NI6] [setup] echo -n "raspberry grape." > "/tmp/quz/f 63"
remote: [your-program] $ echo 'example hello'
remote: [your-program] example hello
remote: [your-program] $ echo hello script
remote: [your-program] hello script
remote: [tester::#NI6] ✓ Received expected response
remote: [your-program] $ echo 'shell world' 'script''example'
remote: [your-program] shell world scriptexample
remote: [tester::#NI6] ✓ Received expected response
remote: [your-program] $ cat '/tmp/quz/f 51' '/tmp/quz/f 12' '/tmp/quz/f 63'
remote: [your-program] mango blueberry.pineapple strawberry.raspberry grape.
remote: [tester::#NI6] Expected prompt ("$ ") but received ""
remote: [your-program] $
remote: [tester::#NI6] Assertion failed.
remote: [tester::#NI6] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

```

And here’s a snippet of my code:

```JavaScript
function prompt() {
  rl.question("$ ", (answer) => {
    const args = parser(answer);
    switch (args[0]) {
      case 'exit':
        if (args.length < 2 || args[1] !== '0') break;
        exit(0);
      case 'echo':
        if (answer.includes('\'')) {
          console.log(answer.substring(answer.indexOf('\''), answer.lastIndexOf('\'')).replaceAll('\'', ''));
        } else {
          console.log(...args.splice(1, args.length));
        }
        break;
      case 'type':
        if (builtin.includes(args[1])) {
          console.log(`${args[1]} is a shell builtin`);
        }
        else {
          const fileName = getFilePath(args[1]);
          if (fileName === null) {
            console.log(`${args[1]}: not found`);
          }
          else {
            console.log(`${args[1]} is ${fileName}`);
          }
        }
        break;
      case 'pwd':
        console.log(process.env.PWD);
        break;
      case 'cd':
        if (args[1] == '~') process.env.PWD = process.env.HOME;
        else if (fs.existsSync(buildPathToDirectory(args[1]))) {
          process.env.PWD = buildPathToDirectory(args[1]);
        }
        else if (fs.existsSync(args[1])) {
          process.env.PWD = `${args[1]}`;
        }
        else console.log(`${args[0]}: ${args[1]}: No such file or directory`);
        break;
      case 'cat':
        let output = "";
        for (let i = 1; i < args.length; i++) {
          if (fs.existsSync(args[i])) output += fs.readFileSync(args[i], 'utf-8');
        }
        output === "" ? output : console.log(output);
        break;
      default:
        const filePath = getFilePath(args[0]);
        if (filePath === null) console.log(`${args[0]}: command not found`);
        else {
          try {
            spawnSync(`${args[0]}`, args.slice(1), { shell: true, stdio: 'inherit' });
          } catch (err) {
            console.error(`Error executing command: ${err.message}`);
            console.error(err.stack);
          }
        }
        break;
    }
    prompt();
  });
}

```

Link to full code on GitHub: [https://github.com/Nicholas-EG/Code-Crafters-Shell/blob/main/main.js](https://github.com/Nicholas-EG/Code-Crafters-Shell/blob/main/main.js)

---

<div class="post-metadata">

**Author:** ![eaverdeja](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/eaverdeja/32/4167_2.png) [@eaverdeja](https://forum.codecrafters.io/u/eaverdeja)\
**Post date:** [March 4, 2025, 7:21pm UTC](https://forum.codecrafters.io/t/struggling-to-understand-whats-causing-this-error/6015/3 "2025-03-04T19:21:38Z")

</div>

Can you try using `process.stdout.write` instead of `console.log` ? I suspect `console.log` is adding a newline behind the scenes.

edit:  
docs don’t lie 😁

> **[Console | Node.js v23.9.0 Documentation](https://nodejs.org/api/console.html#consolelogdata-args)**

> Prints to `stdout` with newline.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [March 9, 2025, 7:22pm UTC](https://forum.codecrafters.io/t/struggling-to-understand-whats-causing-this-error/6015/4 "2025-03-09T19:22:17Z")

</div>

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