[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();
}
[Javascript] Function Expression Ex, Changing Declarations to Expressions的更多相关文章
- javascript ----> Immediately-Invoked Function Expression (IIFE)(翻译)
http://benalman.com/news/2010/11/immediately-invoked-function-expression/ 如果你没有注意到,我对术语有一点点坚持. 所以,在听 ...
- 深入理解 JavaScript Function
1.Function Arguments JavaScript 函数的参数 类型可以是 复杂类型如 Object or Array 和简单类型 String Integer null undefin ...
- 使用"立即执行函数"(Immediately-Invoked Function Expression,IIFE)
一.原始写法 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. function m1(){ //... } function m2(){ // ...
- href="javascript:function()" 和onclick的区别
href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...
- 关于<a href='javascript:function()'>
<a href='javascript:function()'> 这样写是为了让这个链接不要链接到新页面转而执行一段js代码.和onclick能起到同样的效果,一般来说,如果要调用脚本还是 ...
- javascript function对象
<html> <body> <script type="text/javascript"> Function.prototype.get_my_ ...
- Understanding JavaScript Function Invocation and "this"
Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...
- JavaScript function函数种类(转)
转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...
- JavaScript function函数种类介绍
JavaScript function函数种类介绍 本篇主要介绍普通函数.匿名函数.闭包函数 1.普通函数介绍 1.1 示例 ? 1 2 3 function ShowName(name) { ...
随机推荐
- android viewHolder static 静态
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 不是静态内部类 会 持有 外部类的 引用. 就像经常自定义的 适配器 类 作为内部类 ...
- Lunix/Mac下根据最后修改时间复制文件和文件夹,保持原有的目录结构
度娘知道:http://zhidao.baidu.com/link?url=DD47jm6qDgT7yxsnz9e-NC4Fqd33oRoiIwcGLkw5TL4cbf50VKY2IONbHKH0IE ...
- android 从零单排 第一期 按键显示helloworld
啦啦啦- 我是qscqesze 今天开始android的从零单排啦啦啦- 首先从最简单的开始 要求: 程序运行后,单击屏幕上的按键后可以显示一句话,如“Hello World!” 这是一个最基础最基础 ...
- 怎么样退出vi/vim编辑器
怎么样退出vi/vim编辑器 先按 ESC 然后输入 w q :wq 就退出来了
- 如何编写LVS对Real Server的健康状态检测脚本
简介:Linux 虚拟服务器(Linux Virtual Server. LVS),是一个由章文松开发的自由软件.利用KVS可以实现高可用的.可伸缩缩的Web, Mail, Cache和Medial等 ...
- grep查看源代码用法
http://blog.csdn.net/guyongqiangx/article/details/70161189
- H5页面开发笔记(react技术栈)
1.子组件接收父组件的参数,要在子组件的componentDidMount函数中更改当前组件的state,若写在componentWillMount函数中,则会导致初始化界面UI的时候不能得到预期的效 ...
- C#程序集系列08,设置程序集版本
区别一个程序集,不仅仅是程序集名称,还包括程序集版本.程序集公匙.程序集文化等,本篇体验通过界面和编码设置程序集版本. □ 通过Visual Studio设置程序集版本 →右键项目,选择"属 ...
- MVC到底使用哪种方式传递Model,在ViewData、ViewBag、PartialView、TempData、ViewModel、Tuple之间取舍
在"MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple"中,体验了使用不同的方式传递多 ...
- .NET:CLR via C# Assembly Loading
基础知识 Internally, the CLR attempts to load this assembly by using the System.Reflection.Assembly clas ...