In you own words, explain the concepts of scope, hoisting, compartmentalization. Scope is global vs local variables. Hoisting is top to bottom nature of the Javascript document. It is the moving of objects to where they need to be. Strict makes it so it will not do it. Compartmentalization is where each object is placed to make it run the right way.
(function(){ "use strict";
var x = "I'm a local variable"; //console.log(y)
function scopeThis(){ var y = "I'm a global variable"; console.log(x); } scopeThis(); })();
(function() { "use strict";
var goodStanding = true; var monthsActive = 18;
//Do not modify 'name' globaly. var name = null;
var benefit = {} benefit["credit"] = 50; benefit["discount"] = 5;
var accountCheck = function() {
name = "James";
var greeting = function() {
return "Hello " + name + ". Here is the status of your account."
}
function accountStat() {
if (goodStanding == true && monthsActive >= 12) {
return offerDiscount(name);
} else if (goodStanding == false) {
return "Please make a payment within 7 days or your service will be terminated, forever."
} else if (monthsActive <= 12) {
var timeFrame = 12 - monthsActive;
var months;
if (timeFrame == 1) {
months = "month";
} else {
months = "months"
}
return "You are " + timeFrame + " " + months + " from getting a special discount!"
}
function offerDiscount() {
return "Thank you for your loyalty. You've been a member for " + monthsActive + " " + "months . You next bill will reflect a $" + benefit.credit + " credit and a " + benefit.discount + "% discount going forward.";
}
}
return greeting() + " " + accountStat();
} accountCheck(); console.log("#8 accountCheck():", accountCheck()); console.assert(name == "James", "Test failed. You should set 'name' to 'james' from within accountCheck()"); console.assert(accountCheck() == "Hello James. Here is the status of your account. Thank you for your loyalty. You've been a member for 18 months . You next bill will reflect a $50 credit and a 5% discount going forward.", "Test failed. It returned: " + accountCheck());
})();
(function() { "use strict"; var multiply = 2 * 8;
function duplicate() { var multiply = 2 * 10; };
duplicate();
console.log( "multiply", multiply ); console.assert( multiply == 16, "Test failed. How can we isolate duplication()" ); })();