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

[Javascript] Function Expression Ex, Changing Declarations to Expressions的更多相关文章

  1. javascript ----> Immediately-Invoked Function Expression (IIFE)(翻译)

    http://benalman.com/news/2010/11/immediately-invoked-function-expression/ 如果你没有注意到,我对术语有一点点坚持. 所以,在听 ...

  2. 深入理解 JavaScript Function

    1.Function Arguments JavaScript 函数的参数 类型可以是 复杂类型如  Object or Array 和简单类型 String Integer null undefin ...

  3. 使用"立即执行函数"(Immediately-Invoked Function Expression,IIFE)

    一.原始写法 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. function m1(){ //... } function m2(){ // ...

  4. href="javascript:function()" 和onclick的区别

    href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...

  5. 关于<a href='javascript:function()'>

    <a href='javascript:function()'> 这样写是为了让这个链接不要链接到新页面转而执行一段js代码.和onclick能起到同样的效果,一般来说,如果要调用脚本还是 ...

  6. javascript function对象

    <html> <body> <script type="text/javascript"> Function.prototype.get_my_ ...

  7. Understanding JavaScript Function Invocation and "this"

    Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...

  8. JavaScript function函数种类(转)

    转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...

  9. JavaScript function函数种类介绍

    JavaScript function函数种类介绍 本篇主要介绍普通函数.匿名函数.闭包函数 1.普通函数介绍 1.1 示例 ? 1 2 3 function ShowName(name) {     ...

随机推荐

  1. 【HDU 3662】3D Convex Hull

    http://acm.hdu.edu.cn/showproblem.php?pid=3662 求给定空间中的点的三维凸包上有多少个面. 用增量法,不断加入点,把新加的点能看到的面都删掉,不能看到的面与 ...

  2. BZOJ3589动态树

    **错误改了一上午. 先做熟练泼粪 k<=5,因此我们可以模拟这个过程,在线段树上把标记建出来然后pushup时候更新就好了. By:大奕哥 #include<bits/stdc++.h& ...

  3. PHP 大神的十大优良习惯

    1.多阅读手册和源代码 没什么比阅读手册更值得强调的事了–仅仅通过阅读手册你就可以学习到很多东西,特别是很多有关于字符串和数组的函数.就在这些函数里面包括许多有用 的功能,如果你仔细阅读手册,你会经常 ...

  4. brainfuck 解释器

    #include <cstdio>#include <cmath>#include <cstring>#include <ctime>#include ...

  5. nginx 注册为linux系统服务

    #! /bin/sh # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve ...

  6. PHP实现文件下载的核心代码

    PHP实现文件下载的核心代码:

  7. Unity消息

    GameObject关于Message带有三种方法, gameObject.SendMessageUpwards ("test1",4);gameObject.SendMessag ...

  8. jquery的closest方法和parents方法的区别

    今天第一次看到closest方法,以前也从来没用过. 该方法从元素本身开始往上查找,返回最近的匹配的祖先元素. 1.closest查找开始于自身,parents开始于元素父级 2.closest向上查 ...

  9. ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小解决办法

    1.今天写的存储过程在执行过程中,报如下错误. exec PRO_T_008pro_update_add_delete(17,1,1,1,1,45.0,54.0,45.0,45.0,45.0,54.0 ...

  10. MVC客户端使用 Mustache.js把json数据填充到模版中

    使用Mustache的好处是:可以把一些反复用到的html部分定义成Mustache模版,以便多次使用.使用Mustache的大致步骤是: →从后台拿到json数据 →获取前台页面预先定义好Musta ...