[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) { ...
随机推荐
- 当你的静态资源CDN挂掉了该怎么办?
都知道使用静态的CDN引入jQuery等一些js包的时候,会提升网页的性能,那么,如果你引入CDN的地址挂掉了,那么项目同样也会挂掉,所以我们需要在引入的时候添加一个判断.如下: <script ...
- BZOJ 2302: [HAOI2011]Problem c [DP 组合计数]
2302: [HAOI2011]Problem c Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 648 Solved: 355[Submit][S ...
- [NOI2018]你的名字(SAM+线段树合并)
考虑l=1,r=n的68分,对S和T建SAM,对T的SAM上的每个节点,计算它能给答案带来多少贡献. T上节点x代表的本质不同的子串数为mx[x]-mx[fa[x]],然后需要去掉所代表子串与S的最长 ...
- [CC-CHEFINV]Chef and Swaps
[CC-CHEFINV]Chef and Swaps 题目大意: 长度为\(n(n\le2\times10^5)\)的数列,\(q(q\le2\times10^5)\)次询问,每次问交换\(A_x\) ...
- MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传
本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 ...
- 关于WEB集群中文件服务器的讨论
原文地址: http://blog.itpub.net/29806344/viewspace-1364778/ 在WEB集群中一般都要上传和删除图片.小规模的时候,图片放在本地,再通过同步方式来保持一 ...
- arcgis python添加几何属性
import arcpy import numpy import math def AddGeometryAttributes(fc, geomProperties, lUnit, aUnit, cs ...
- hadoop函数说明图
- ndk 开发
5.用NDK来编译程序 1. 现在我们用安装好的NDK来编译一个简单的程序吧,我们选择ndk自带的例子hello-jni,我的位于E:/android-ndk-r5/samples/hello-jn ...
- 为 Tomcat 安装 apr
apr 官方介绍: Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术. APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTT ...