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");
}