// Scope Homework Solution: Who Dunnit // Episode 1 const scenario = { murderer: 'Miss Scarlet', room: 'Library', weapon: 'Rope' }; const declareMurderer = function() { return `The murderer is ${scenario.murderer}.`; }; const verdict = declareMurderer(); console.log(verdict); // Output: The murderer is Miss Scarlet. // Reason: declareMurderer is called, which returns a string that refers to the murderer property on the scenario object. // // Episode 2 const murderer = 'Professor Plum'; const changeMurderer = function() { murderer = 'Mrs. Peacock'; }; const delcareMurderer = function() { return `The murderer is ${murderer}.`; } changeMurderer(); const verdict = declareMurderer(); console.log(verdict); // Output: TypeError // Reason: The variable murderer is declared with the const keyword, so it cannot be resigned. When the changeMurderer function is called, it attempts to reassign the murderer variable, producing the type error Assignment to constant variable. // // Episode 3 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); // Output: First Verdict: The murderer is Mrs. Peacock. Second Verdict: The murderer is Professor Plum. // Reason: The delcareMurderer function is called, which creates a new local variable, murderer with the value of 'Mrs. Peacock' and returns a string that refers to local variable. This does not effect the initial murderer variable, so when the second verdict accesses the outer variable, it is still 'Professor Plum'. // // Episode 4 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}.`); // Output: The suspects are Miss Scarlet, Professor Plum, Colonel Mustard. Suspect three is Mrs. Peacock. Reason: The initial suspect variables are declared. suspectThree has the value 'Mrs. Peacock'. When the declareAllSuspects function is called, it creates a new local variable suspectThree with the value 'Colonel Mustard' and does not effect the initial variable of the same name. The string returned by declareAllSuspects refers to the two initial variables suspectOne and suspectTwo and the local variable suspectThree. The second log refers to the unchanged initial variable, suspectThree with the value 'Mrs. Peacock'. // // Episode 5 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); // Output: The weapon is the Revolver. Reason: changeWeapon is called, changing the scenario's weapon property to 'Revolver'. delclareWeapon is then called, returning a string that refers to the scenario's weapon property. // // Note: The scenario variable is declared using the const keyword so it cannot be reassigned. However an object is mutable, so its properties can be modified without it being a reassignment. // // Episode 6 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); // Output: The murderer is Mrs. White. Reason: changeMurder is called, which first reassigns murderer to have the value 'Mr. Green', then calls a second function, plotTwist, which reassigns murderer to have the value 'Mrs. White'. declareMurderer is then called returning a string that refers to murderer. // // Episode 7 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); // Output: The murderer is Mr. Green. Reason: changeMurderer is called, which first reassigns murderer to have the value 'Mr. Green'. It then calls plotTwist that creates a new local variable murderer with the value 'Colonel Mustard', which does not effect the initial murderer variable. unexpectedOutcome is then called which reassigns the local variable in the plotTwist function to be 'Miss Scarlet', but again, does not effect the initial murderer variable. When declareMurderer is called, it returns a string that refers to the initial murderer variable, which has only been reassigned once. // // Episode 8 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); // // Output: The murderer is Colonel Mustard. Reason: changeScenario is called which update's scenario's murderer property to 'Mrs. Peacock' and its room property to 'Dining Room'. Next plotTwist is called and checks if the scenario's room property is equals to 'Dining Room', which it is, so updates the scenario's 'murderer property to be 'Colonel Mustard'. Then unexpectedOutcome is called, which checks if the scenario's murderer property is equal to 'Colonel Mustard', which it is, so it updates the scenario's weapon property to be 'Candle Stick'. Then declareWeapon is called, which returns a string referring to the updated scenario's weapon property. // // Episode 9 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); // Output: The murderer is Professor Plum. Reason: A variable murderer is declared with the value of 'Professor Plum'. The an if statement checks if murderer is equal to 'Professor Plum', which it is, so it creates a new local murderer variable with the value of 'Mrs. Peacock'. Because variables declared with the let and const keywords are block scoped, is does not effect the initial murder variable. When declareMurderer is called, it returns a string that refers to the initial murder variable.