[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) { ...
随机推荐
- 转MySQL常见错误分析与解决方法总结
一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分析:这说明“localhost”计算机 ...
- iptables配置允许mysql远程访问
vi /etc/sysconfig/iptables iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCE ...
- 撩课-Java每天5道面试题第11天
86.如何获得高效的数据库逻辑结构? 从关系数据库的表中 删除冗余信息的过程 称为数据规范化, 是得到高效的关系型数据库表的逻辑结构 最好和最容易的方法. 规范化数据时应执行以下操作: 1.将数据库的 ...
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心
E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- Linux常用命令&定位生产报错日志
1. cd / 到根目录下 2. cd .. 返回上层目录 3.ls 显示当前目录有哪些文件 4. pwd 显示当前目录 5. ps -ef|grep tomcat7 查看当前运行进程 6. kill ...
- LAMP架构之NFS
需求分析: 前端需支持更大的访问量,单台Web服务器已无法满足需求了,则需扩容Web服务器: 虽然动态内容可交由后端的PHP服务器执行,但静态页面还需要Web服务器自己解析,那是否意味着多台Web服务 ...
- Accessing an element's parent with ElementTree(转)
Today I ran across a situation where I needed to programmatically remove specific elements from a KM ...
- hdu 4647 Another Graph Game,想到了就是水题了。。
题目是给一个无向图,其中每个节点都有点权,边也有边权,然后就有2个小朋友开始做游戏了ALICE &BOB 游戏规定ALICE 先行动然后是BOB,然后依次轮流行动,行动时可以任意选取一个节点并 ...
- C# 去掉webapi返回json所带的转义字符
反序列换报错: {"Error converting value \"{\"Result\":true,\"Code\":\"\& ...