- Understand function scope
- Know the difference between the let and const keywords
Using your knowledge about scope and variable declarations in JavaScript, look at the following code snippets and predict what the output or error will be and why.
Solution is: Miss Scarlet
const scenario = {
murderer: 'Miss Scarlet',
room: 'Library',
weapon: 'Rope'
};
const declareMurderer = function() {
return `The murderer is ${scenario.murderer}.`;
}
const verdict = declareMurderer();
console.log(verdict);
Solution would be Professor Plum because we cannot modify const murderer, but the code crash because of attempting this change.
const murderer = 'Professor Plum';
const changeMurderer = function() {
murderer = 'Mrs. Peacock';
}
const declareMurderer = function() {
return `The murderer is ${murderer}.`;
}
changeMurderer();
const verdict = declareMurderer();
console.log(verdict);
First Verdict: Mrs. Peacock Second Verdict: Professor Plum
let murderer = 'Professor Plum';
const declareMurderer = function() {
let murderer = 'Mrs. Peacock';
return `The murderer is ${murderer}.`;
}
const firstVerdict = declareMurderer();
console.log('First Verdict: ', firstVerdict);
const secondVerdict = `The murderer is ${murderer}.`;
console.log('Second Verdict: ', secondVerdict);
Solution is [Miss Scarlet, Professor Plum, Colonel Mustard] Second log is Mrs. Peacock
let suspectOne = 'Miss Scarlet';
let suspectTwo = 'Professor Plum';
let suspectThree = 'Mrs. Peacock';
const declareAllSuspects = function() {
let suspectThree = 'Colonel Mustard';
return `The suspects are ${suspectOne}, ${suspectTwo}, ${suspectThree}.`;
}
const suspects = declareAllSuspects();
console.log(suspects);
console.log(`Suspect three is ${suspectThree}.`);
Solution is Revolver
const scenario = {
murderer: 'Miss Scarlet',
room: 'Kitchen',
weapon: 'Candle Stick'
};
const changeWeapon = function(newWeapon) {
scenario.weapon = newWeapon;
}
const declareWeapon = function() {
return `The weapon is the ${scenario.weapon}.`;
}
changeWeapon('Revolver');
const verdict = declareWeapon();
console.log(verdict);
Solution is Mrs. White
let murderer = 'Colonel Mustard';
const changeMurderer = function() {
murderer = 'Mr. Green';
const plotTwist = function() {
murderer = 'Mrs. White';
}
plotTwist();
}
const declareMurderer = function () {
return `The murderer is ${murderer}.`;
}
changeMurderer();
const verdict = declareMurderer();
console.log(verdict);
Solution is Mr. Green
let murderer = 'Professor Plum';
const changeMurderer = function() {
murderer = 'Mr. Green';
const plotTwist = function() {
let murderer = 'Colonel Mustard';
const unexpectedOutcome = function() {
murderer = 'Miss Scarlet';
}
unexpectedOutcome();
}
plotTwist();
}
const declareMurderer = function() {
return `The murderer is ${murderer}.`;
}
changeMurderer();
const verdict = declareMurderer();
console.log(verdict);
Solution is Candle Stick
const scenario = {
murderer: 'Mrs. Peacock',
room: 'Conservatory',
weapon: 'Lead Pipe'
};
const changeScenario = function() {
scenario.murderer = 'Mrs. Peacock';
scenario.room = 'Dining Room';
const plotTwist = function(room) {
if (scenario.room === room) {
scenario.murderer = 'Colonel Mustard';
}
const unexpectedOutcome = function(murderer) {
if (scenario.murderer === murderer) {
scenario.weapon = 'Candle Stick';
}
}
unexpectedOutcome('Colonel Mustard');
}
plotTwist('Dining Room');
}
const declareWeapon = function() {
return `The weapon is ${scenario.weapon}.`
}
changeScenario();
const verdict = declareWeapon();
console.log(verdict);
Solution is Professor Plum
let murderer = 'Professor Plum';
if (murderer === 'Professor Plum') {
let murderer = 'Mrs. Peacock';
}
const declareMurderer = function() {
return `The murderer is ${murderer}.`;
}
const verdict = declareMurderer();
console.log(verdict);
Make up your own episode! [Check file episodes/custom_episode.js]