Among Us: The Impostor Code (A Story of JavaScript Arrays)

Among Us: The Impostor Code (A Story of JavaScript Arrays)

ยท

3 min read

It was a normal day on the Skeld, the spaceship traveling through space. 10 crewmates were working hard, fixing wires, downloading data, and refueling engines.

But there was a problemโ€ฆ Two impostors were among them! They had hacked the shipโ€™s security system and were deleting important files. The crewmates had to restore the system using JavaScript arrays before it was too late!

๐Ÿ›  Chapter 1: Assigning Crewmates (push())

The game started. The computer randomly added players to the crew.

let crewmates = ["Red", "Blue", "Green"];
crewmates.push("Yellow");  
console.log(crewmates);
// Output: ["Red", "Blue", "Green", "Yellow"]

โœ… Yellow joins the spaceship!

๐Ÿ›  Chapter 2: A Crewmate is Eliminated (pop())

An emergency meeting! Red was found dead in Electrical.

let eliminated = crewmates.pop();  
console.log(crewmates);
// Output: ["Red", "Blue", "Green"]
console.log(eliminated);
// Output: "Yellow"

โœ… Yellow is ejected! But was he the impostor?

๐Ÿ›  Chapter 3: New Captain Takes Charge (unshift())

"Someone needs to lead us!" said Blue.

crewmates.unshift("Blue");  
console.log(crewmates);
// Output: ["Blue", "Red", "Green"]

โœ… Blue takes charge of the mission.

๐Ÿ›  Chapter 4: A Crewmate is Lost (shift())

While fixing wires, Green disappeared!

let missingCrewmate = crewmates.shift();  
console.log(crewmates);
// Output: ["Red", "Green"]
console.log(missingCrewmate);
// Output: "Blue"

โœ… Was Green the impostor? Or is something more sinister happening?

๐Ÿ›  Chapter 5: Scanning for Impostors (map())

The remaining crewmates decided to check if anyone was acting suspicious.

let scannedCrewmates = crewmates.map(player => player + " (Scanned)");
console.log(scannedCrewmates);
// Output: ["Red (Scanned)", "Green (Scanned)"]

โœ… No impostors detectedโ€ฆ yet!

๐Ÿ›  Chapter 6: Searching for Safe Crewmates (filter())

"We need to separate real crewmates from impostors!" said Blue.

let allPlayers = ["Red", "Blue", "Impostor1", "Impostor2"];
let realCrewmates = allPlayers.filter(player => !player.includes("Impostor"));
console.log(realCrewmates);
// Output: ["Red", "Blue"]

โœ… The impostors are still hiding!

๐Ÿ›  Chapter 7: Counting Completed Tasks (reduce())

The crew checked how many tasks were left to complete before winning.

let tasksDone = [2, 3, 5, 4];  
let totalTasks = tasksDone.reduce((total, task) => total + task, 0);
console.log(totalTasks);
// Output: 14

โœ… Only a few tasks left! Can they finish before the impostors strike?

๐Ÿ›  Chapter 8: Finding the Impostor (find())

Cameras caught someone venting! The crew checked the logs.

let players = [
  { name: "Red", role: "Crewmate" },
  { name: "Blue", role: "Crewmate" },
  { name: "Yellow", role: "Impostor" }
];

let impostor = players.find(player => player.role === "Impostor");
console.log(impostor);
// Output: { name: "Yellow", role: "Impostor" }

โœ… Yellow is the impostor! Time to vote!

๐Ÿ›  Chapter 9: Finding the Last Task (indexOf())

The crewmates had one last task to finish before winning!

let tasks = ["Fix Wires", "Upload Data", "Start Reactor"];
let lastTask = tasks.indexOf("Start Reactor");
console.log(lastTask);
// Output: 2

โœ… Run to Reactor Room before the impostor sabotages!

๐Ÿ›  Chapter 10: Restoring the System (slice())

The crewmates finally recovered the lost security files.

let systemData = ["Start", "Load", "Restore", "Execute"];
let finalSteps = systemData.slice(1, 3);
console.log(finalSteps);
// Output: ["Load", "Restore"]

โœ… The system is fixed! Time to eject the impostor.

๐Ÿš€ The Final Vote

An emergency meeting was called.

"Yellow is the impostor!"

Everyone voted. Yellow was ejected.

console.log("Impostor was ejected. Crew wins!");

๐ŸŽ‰ Crewmates Win! The ship is saved! ๐ŸŽ‰

ย