Share
Explore

The answer to the F13 Hackathon Challenge

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:


megaphone

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
Using Callbacks
javascriptCopy code
function fetchData(callback) {
setTimeout(() => {
callback('Data fetched after 2 seconds.');
}, 2000);
}
fetchData(data => console.log(data));

Using Promises
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
var is Function Scoped
javascriptCopy code
function testVar() {
if (true) {
var x = 10;
}
console.log(x); // 10
}
testVar();

let and const are Block Scoped
javascriptCopy code
function testLetConst() {
if (true) {
let y = 10;
const z = 20;
}
console.log(y, z); // ReferenceError: y is not defined
}
testLetConst();

var Hoisting
javascriptCopy code
function hoistingExample() {
console.log(v); // undefined
var v = 50;
}
hoistingExample();

4. Closures
Basic Closure Example
javascriptCopy code
function outerFunction() {
let outerVariable = 'I am outside!';
return function innerFunction() {
console.log(outerVariable);
};
}
const closureFunction = outerFunction();
closureFunction(); // 'I am outside!'

Closure with Private Variables
javascriptCopy code
function counter() {
let count = 0;
return function() {
return count++;
};
}
const myCounter = counter();
console.log(myCounter()); // 0
console.log(myCounter()); // 1

Closure with Set and Get Methods
javascriptCopy code
function createPerson() {
let name = 'John Doe';
return {
setName: function(newName) {
name = newName;
},
getName: function() {
return name;
}
};
}
const person = createPerson();
console.log(person.getName()); // 'John Doe'
person.setName('Jane Smith');
console.log(person.getName()); // 'Jane Smith'

Closures with Event Listeners
javascriptCopy code
document.getElementById('myButton').addEventListener('click', (function() {
let count = 0;
return function() {
count++;
console.log(`Button clicked ${count} times.`);
};
})());

I hope this provides a clear understanding of these topics!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.