the correct answer to the challenge is ... Let's go through the challenges one by one:
Challenge 1: The Ghostly Variables
javascriptCopy code
let ghost = "Phantom";
let goblin = "Hobgoblin";
let ghoul = "Nightmare";
console.log(ghost, goblin, ghoul);
Fragment: A mysterious 1.
Challenge 2: Eerie Arrays
javascriptCopy code
let spookyItems = ["bat", "witch", "moon", "spell", "potion"];
spookyItems.unshift("zombie");
spookyItems.push("skeleton");
Fragment: The haunting 0.
Challenge 3: Sinister Strings
javascriptCopy code
function reverseString(str) {
return str.split("").reverse().join("");
}
Fragment: A ghostly 1.
Challenge 4: Terrors of Ternary
javascriptCopy code
let number = 4; // or any number
let result = number % 2 === 0 ? "Spooky" : "Eerie";
console.log(result);
Fragment: The elusive 0.
Challenge 5: Bewitched Booleans
javascriptCopy code
function bewitchedBool(a, b) {
return !(a === b);
}
Fragment: An ethereal 1.
Challenge 6: Demonic Data Types
javascriptCopy code
function identifyType(variable) {
if (Array.isArray(variable)) {
return "array";
} else if (variable === null) {
return "null";
} else {
return typeof variable;
}
}
Fragment: The dark 0.
Challenge 7: Recursive Riddles
javascriptCopy code
function countO(string, index = 0, count = 0) {
if (index === string.length) {
return count;
}
if (string[index] === 'o') {
count++;
}
return countO(string, index + 1, count);
}
Fragment: A spectral 1.
Challenge 8: Cryptic Callbacks
javascriptCopy code
function executeAfterDelay(callback) {
setTimeout(callback, 3000);
}
Fragment: The phantom 0.
Challenge 9: Asynchronous Apparitions
javascriptCopy code
async function sealAfterDelay() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Sealed"), 2000);
});
let result = await promise;
console.log(result);
}
Fragment: A chilling 1.
Challenge 10: Final Foray with Functions
javascriptCopy code
function returnFunction() {
return function() {
console.log("The portal is sealing...");
}
}
Fragment: The final piece, 0.
The Final Ritual Key:
The correct sequence from the completed challenges is 1 0 1 0 1 0 1 0 1 0.
Students can use this sequence as the final key in the ritual to "seal" the portal.
More Coding Drills:
Let's dive into these topics step by step.
1. Temporal Dead Zone (TDZ) and Hoisting
Illustrating TDZ with let javascriptCopy code
console.log(a); // ReferenceError: Cannot access 'a' before initialization
let a = 10;
Illustrating TDZ with const javascriptCopy code
console.log(b); // ReferenceError: Cannot access 'b' before initialization
const b = 20;
2. Embracing Async/Await, Promises, and Callbacks
javascriptCopy code
function fetchData(callback) {
setTimeout(() => {
callback('Data fetched after 2 seconds.');
}, 2000);
}
fetchData(data => console.log(data));
javascriptCopy code
function fetchData() {
return new Promise(resolve => {
setTimeout(() => {
resolve('Data fetched after 2 seconds.');
}, 2000);
});
}
fetchData().then(data => console.log(data));
Using Async/Await with Promises javascriptCopy code
async function fetchAndDisplayData() {
const data = await fetchData();
console.log(data);
}
fetchAndDisplayData();
function fetchData() {
return new Promise(resolve => {
setTimeout(() => {
resolve('Data fetched after 2 seconds.');
}, 2000);
});
}
3. let and const vs. var: Block Scoping and Hoisting