[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();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值