Node Scripts Explanation
To check if it works, navigate to this directory in your command prompt.
cd ...\eroberts_site\projects\eroberts_scripts\node-scripts
And type:
node index.js
The desired output:
=== Magical Worlds Defense ===
Aligning chakras...
Mordor status: Danger, invasion!
Sword Coast status: Danger, invasion!
Mushroom Kingdom status: Danger, invasion!
Midgar status: Danger, invasion!
Hyrule status: Danger, invasion!
Casted fireball! Boooom! Huge explosion!
You saved Midgar!
=== Magical Worlds Defense ===
So, the first file for this script is message.js. In here, I created a simple function that just passes a parameter to the console.
function narrate(message) {
console.log(`${message}`);
}
Then all I had to do was export it so I can use it on other files. At first I ran into a little hiccup when I tried exporting narrate() instead of just narrate. I also export every single script except index.js.
module.exports = narrate;
After that I made function.js. It has this function with lots of switch cases. It can cast different spells which result in different string outcomes depending on the parameter you pass with the function.
function spellCast(spell) {
switch (spell) {
case "fireball":
console.log("Casted fireball! Boooom! Huge explosion!");
console.log("You saved Midgar!")
break;
case "heal":
console.log("Casted heal. You're feeling those healing energies!");
console.log("You saved the Mushroom Kingdom!");
break;
case "spark":
console.log("Casted sparks! Zzzzzap!");
console.log("You saved Hyrule!");
break;
default:
console.log("The spell backfired. You suck.");
console.log("You saved Mordor!");
break;
}
}
Then I made loop.js. It parses through an array called regions that is filled with strings. Then, depending on the parameter passed with the function, it will pass different string outcomes. To do this, I used a conditional statement.
function runMagicalScan(state) {
const regions = ['Mordor', 'Sword Coast', 'Mushroom Kingdom', 'Midgar', 'Hyrule'];
for (let i=0; i < regions.length; i++) {
const safety = state === "safe" ? "Safe and secure." : "Danger, invasion!";
console.log(`${regions[i]} status: ${safety}`)
}
}
Finally, index.js calls in the functions we made in past files and uses them to help digital magical worlds defend against evil.
const narrate = require("./message");
const spellCast = require("./function");
const runMagicalScan = require("./loop");
narrate("=== Magical Worlds Defense ===");
narrate("Aligning chakras...");
runMagicalScan("danger");
spellCast("fireball");
narrate("=== Magical Worlds Defense ===");
I had fun making this. JavaScript makes a lot more sense than other programming languages I've tried. Try casting other spells to save different regions.