Bittorrent challenge - error at #AHI

I’m stuck on Stage #AH1.

I’ve tried many different logics.But everyone gives me the same output.

Here are my logs:

 Running tests for Stage #AH1 (Decode bencoded lists)
[tester::#AH1] Running ./your_bittorrent.sh decode le
[tester::#AH1] Expected output: []
[your_program] Logs from your program will appear here!
[your_program] []
[tester::#AH1] Running ./your_bittorrent.sh decode l6:orangei901ee
[tester::#AH1] Expected output: ["orange",901]
[your_program] Logs from your program will appear here!
[your_program] ["orange",901]
[tester::#AH1] Running ./your_bittorrent.sh decode lli901e6:orangeee
[tester::#AH1] Expected output: [[901,"orange"]]
[your_program] Logs from your program will appear here!
[your_program] /app/app/main.js:34
[your_program]         throw new Error(`Unexpected character ${value} at position ${i}`);
[your_program]         ^
[your_program] 
[your_program] Error: Unexpected character e at position 14
[your_program]     at decodeBencode (/app/app/main.js:34:15)
[your_program]     at decodeBencode (/app/app/main.js:30:28)
[your_program]     at main (/app/app/main.js:66:32)
[your_program]     at Object.<anonymous> (/app/app/main.js:73:1)
[your_program]     at Module._compile (node:internal/modules/cjs/loader:1368:14)
[your_program]     at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
[your_program]     at Module.load (node:internal/modules/cjs/loader:1205:32)
[your_program]     at Module._load (node:internal/modules/cjs/loader:1021:12)
[your_program]     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
[your_program]     at node:internal/main/run_main_module:28:49
[your_program] 
[your_program] Node.js v21.7.3
[tester::#AH1] Application didn't terminate successfully without errors. Expected 0 as exit code, got: 1
[tester::#AH1] Test failed

And here’s a snippet of my code:

function decodeBencode(bencodedValue) {
  if (bencodedValue[0] === "l" && bencodedValue[bencodedValue.length - 1] === "e") {
    // Handle list: starts with 'l' and ends with 'e'
    const list = [];
    let i = 1;
    const lastIndex = bencodedValue.length - 1;

    while (i < lastIndex) {
      const value = bencodedValue[i];

      if (value === "i") {
        // Handle integer (starts with 'i' and ends with 'e')
        const end = bencodedValue.indexOf("e", i);
        list.push(parseInt(bencodedValue.substring(i + 1, end)));
        i = end + 1;
      } else if (!isNaN(value)) {
        // Handle string (starts with a number, followed by ':' and the string)
        const colonIndex = bencodedValue.indexOf(":", i);
        const length = parseInt(bencodedValue.substring(i, colonIndex), 10);
        list.push(bencodedValue.substring(colonIndex + 1, colonIndex + 1 + length));
        i = colonIndex + 1 + length;
      } else if (value === "l") {
        // Handle nested list (recursive call)
        const nestedList = decodeBencode(bencodedValue.substring(i));
        list.push(nestedList[0]); // Push the decoded nested list
        i += nestedList[1]; // Update index to continue after the nested list
      } else {
        throw new Error(`Unexpected character ${value} at position ${i}`);
      }
    }
    return list;
  }

  if (!isNaN(bencodedValue[0])) {
    // Handle string encoding (length prefix followed by the string)
    const colonIndex = bencodedValue.indexOf(":");
    const length = parseInt(bencodedValue.substring(0, colonIndex), 10);
    return bencodedValue.substring(colonIndex + 1, colonIndex + 1 + length);
  }

  throw new Error("Invalid Bencode input");
}

Hi @SREERAJ089, could you upload your code to GitHub and share the link?

[SREERAJ089/bittorrent-javascript]

Hi @SREERAJ089, I tried running your code against the previous stages, but it’s actually no longer passing the second stage #EB4 (Decode bencoded integers).

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

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.