[Javascript] Function Expression Ex, Changing Declarations to Expressions
Inside the Haunted Hickory House file, developers for the Forest of Function Expressions Theme Park have created a declared function called forestFright
. They’ve decided not to keep the function in memory, however, but instead only use it if fierce animals make their way into the HHH.
Change the function to an anonymous function expression and assign it to a variable called runAway
.
function forestFright(){ var toAlert = ""; for(var i = 0; i<5; i++){ toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n"; } alert(toAlert); }
Changed to: Answer
var runAway = function(){ var toAlert = ""; for(var i = 0; i<5; i++){ toAlert = toAlert + "Lions, Tigers, and Bears, Oh My!!\n"; } alert(toAlert); };
The devs at the Death-Defying Dogwoods have determined a specific formula for the quantifiable amount of Fear generated at the DDD. (This is important to know if you are running a theme park, ya know). Their mystery formula is based on the amount of people at the Dogwoods, the current precipitation, and, as might be expected, the amount of sharks. Yes. Sharks.
Here is their genius formula.
var fearGenerated = function ( numPeeps, rainInInches, numSharks){ var rainFear = numPeeps * rainInInches; var sharkFear = numSharks * numSharks * numSharks; var totalFear = sharkFear + rainFear; return totalFear; };
The goal of the developers is to have the amount of Fear generated to be no more than 400, but no less than 100 (they’re running a business, for pity’s sake!).
They’ve asked you to analyze the formula, and then assign values to the variables in hauntedHickoryHouse.js on the right, where the clickable entry space is. Additionally, they need you to then call the function expression on the next line, using your new values as parameters, and store the result in a variable called fear
.
Answer:
var people = 10; var rain = 10; var sharks = 5; var fearGenerated = function ( numPeeps, rainInInches, numSharks){ var rainFear = numPeeps * rainInInches; var sharkFear = numSharks * numSharks * numSharks; var totalFear = sharkFear + rainFear; return totalFear; }; var fear = fearGenerated(people, rain, sharks);
Welp, it stands to reason that some people might not want to experience the Hickory Haunted House if the fear is significantly elevated on that day.
The devs need you to check the already-generated fear
value for that day, and decide whether it is LOW, MEDIUM, or HIGH. Depending on the severity of the fear, they want a specific confirmation message built inside a function expression, and they want this function expression stored in a variable called fearMessage
. (They are sort of picky about implementation).
Then, this fearMessage
variable should be passed into a declared function called confirmRide
, which is provided for you. Additionally, the results of calling confirmRide
should be stored in a variable called startRide
(i.e., true
, or false
, from the user’s confirmation).
The confirmation messages should take on the following formats:
For fear levels less than 200:
Fear Level: LOW Should be no problem at all...mwahaha. Still wanna ride?
For fear levels 200 through and including 300:
Fear Level: MEDIUM You may want to rethink this one. MWAHAHA! Think you'll make it?
For fear levels above 300 and up to 400:
Fear Level: HIGH ABANDON ALL HOPE!! Have a death wish?
var fear = fearGenerated(numPeeps, rainInInches, numSharks); var numPeeps= 10; var rainInInches = 10; var numSharks = 5; var fearMessage; var MIN = 100; var LOW = 200; var MEDIUM = 300; var FEAR = 400; var fearMessage = function(){ var LOW_MSG = 'Fear Level: LOW'+ 'Should be no problem at all...mwahaha.'+ 'Still wanna ride?'; var MED_MSG = 'Fear Level: MEDIUM'+ 'You may want to rethink this one. MWAHAHA!'+ 'Think you\'ll make it?'; var FEAR_MSG = 'Fear Level: HIGH'+ 'ABANDON ALL HOPE!!'+ 'Have a death wish?'; if(fear >= MIN && fear < LOW){ return LOW_MSG; }else if(fear >= LOW && fear < MEDIUM){ return MED_MSG; }else if(fear >= MEDIUM && fear < FEAR){ return FEAR_MSG; }else{ return ""; } }; var startRide = confirmRide(fearMessage); function confirmRide( confirmToGo ){ return confirmToGo(); }