[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) { ...
随机推荐
- curl_get和curl_post,伪造请求头,绕过防盗链下载文件
//curl-get function curl_get($url, $timeout = 10) { $ch = curl_init();//初始化 curl_setopt($ch, CURLOPT ...
- Jmeter实现webservice的接口测试
前提条件 测试的URL:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx 测试接口:getSupportCityString 获取城市的编码:htt ...
- [转载] 使用Kettle进行数据迁移(ETL)
由于开发新的系统,需要将之前一个老的C/S应用的数据按照新的数据设计导入到新库中.此过程可能涉及到表结构不一致.大数据量(千万级,甚至上亿)等情况,包括异构数据的抽取.清洗等等工作.部分复杂的工作需要 ...
- java的(PO,VO,TO,BO,DAO,POJO)解释1
java的(PO,VO,TO,BO,DAO,POJO)解释 O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定 ...
- ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th
MSDN===http://msdn.microsoft.com/en-us/library/aa479332.aspx PROJECT==https://code.google.com/p/elma ...
- 最快的BT软件rtorrent Step by Step指南
原文地址:http://forum.ubuntu.org.cn/viewtopic.php?t=165069 rtorrent是linux下最快的bt下载软件,由于支持DHT网络,可以很好的于迅雷和B ...
- 0x80070570 文件或目录损坏且无法读取 CHKDSK 修复方法
错误 0x80070570: 文件或目录损坏且无法读取. 不要太担心是出现了磁盘坏道,也许只是小小的存储问题.解决方法很简单,用chsdsk命令即可. 方法如下: 开始--运行--输入cmd--输入c ...
- JS图片自己主动轮换效果实现
今天不在状态,五一快到了,俺就特想玩了.好了,天色已晚,闲话不多说,看下用javaScript 实现的图片自己主动轮换效果,先看图片 以下是详细的代码,还是比較简单的. <!DOCTYPE ht ...
- 页面中基于JSTL标签调用函数--之${fn:}内置函数
调用这样一个头文件<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " ...
- Android只播放gif动画
使用easygifanimator软件把gif动画打散为图片. 第一步:先上图片素材,以下素材放到res/drawable目录下: 转:http://blog.csdn.net/aminfo/arti ...