They’ve got a problem with their existing code, which tries to use a closure. Check it out:

function assignLaser( shark, sharkList ){
var stationAssignment;
for(var i = 0; i<sharkList.length; i++){
if(shark == sharkList[i]){
stationAssignment = function(){
alert("Yo, " +
shark +
"!\n" +
"Visit underwater strapping station " +
i +
" for your sweet laser.\n" +
"'Bout to get real up in here."
);
};
}
}
return stationAssignment;
}

Solution ONE:

Remove stationAssignment here, it will hold the i variable until the loop end, so return the fucntion immediatly when you find the value.

function assignLaser( shark, sharkList ){
for(var i = 0; i<sharkList.length; i++){
if(shark == sharkList[i]){
return function(){
alert("Yo, " +
shark +
"!\n" +
"Visit underwater strapping station " +
i +
" for your sweet laser.\n" +
"'Bout to get real up in here."
);
};
}
}
}

Solution TWO:

var sharkList = ["yrr", "wff", "eff", "gee"];
function makerLaserAssigner(sharkList){
return function(shark){
for(var i = 0; i<sharkList.length; i++){
if(shark == sharkList[i]){
alert("Yo, " +
shark +
"!\n" +
"Visit underwater strapping station " +
i +
" for your sweet laser.\n" +
"'Bout to get real up in here."
);
}
}
};
}
var getSharkLaser = makerLaserAssigner(sharkList);
getSharkLaser("eff");

--------------------------------EX-----------------------

The Dev Girls now need a target assignment for each shark. For your reference, the lists of sharks and targets is as follows:

var listOfSharks = ["Sea Pain", "Great Wheezy",
"DJ Chewie", "Lil' Bitey",
"Finmaster Flex", "Swim Khalifa",
"Ice Teeth", "The Notorious J.A.W."];
var listOfTargets = ["icicle bat", "snow yeti",
"killer penguin", "frost tiger",
"polar bear", "iceberg",
"blue witch", "wooly mammoth"];

The Devs want to use the following function call whenever they need to find the right target for any shark:

var getTargetFor = makeTargetAssigner(  listOfSharks,
listOfTargets );
getTargetFor("Ice Teeth");

Here’s an example of the pop-up alert that the devs would like their call to getTargetFor to produce:

What up, Ice Teeth!
There've been blue witch sightings in our 'hood!
Time for a swim-by lasering, homie!

*Note: A shark’s list index matches the index of the target it is supposed to eliminate.

YOUR goal is to build out the makeTargetAssigner function with a useful closure, so that it returns a function that can be used in the manner the devs are asking for. To help us check your work more efficiently, use shark as the parameter for a shark’s name in your closure function. You may want to use your own browser’s console to test a few inputs!

Answer:

function makeTargetAssigner( sharks, targets ){
return function(shark){
var s_i = sharks.indexOf(shark);
var t_i = s_i;
var target = targets[t_i];
alert("What up,"+" "+
shark+
"!\n"+
"There've been"+" "+
target+
" sightings in our 'hood!\n"+
"Time for a swim-by lasering, homie!");
};
}

[Javascript] Closure Cove, Common mistake的更多相关文章

  1. [Javascript] Closure Cove, 1

    Returning a function from a function, complete with variables from an external scope, is called a cl ...

  2. common mistake of closure in loops

    [common mistake of closure in loops] 下例中item引用的始终是最后一个值. function showHelp(help) { document.getEleme ...

  3. 如何理解javascript closure ?

    接触过javascript的人应该听过闭包(closure),有一种观点认为是闭包赋予了javascript的强大能力,也赋予了它具备OOP的特征.既然javascript closure如此重要,那 ...

  4. [Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure

    function buildCoveTicketMarker(transport){ var passengerNumber = 0; return function(name){ passenger ...

  5. javascript closure 闭包 事件绑定

    先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...

  6. About javascript closure

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures?redirectlocale=en-US&redi ...

  7. [Javascript] Introducing Reduce: Common Patterns

    Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operation ...

  8. js闭包之初步理解( JavaScript closure)

    闭包一直是js中一个比较难于理解的东西,而平时用途又非常多,因此不得不对闭包进行必要的理解,现在来说说我对js闭包的理解. 要理解闭包,肯定是要先了解js的一个重要特性, 回想一下,那就是函数作用域, ...

  9. javascript closure

    http://www.jibbering.com/faq/notes/closures/ http://hi.baidu.com/bluedream_119/item/938dcd082b1e1880 ...

随机推荐

  1. AtCoder Grand Contest 019 F-yes or no

    AtCoder Grand Contest 019 F-yes or no 解题思路: 考虑一个贪心策略,假设当前还有 \(x\) 道 \(\text{yes}\) 和 \(y\) 道 \(\text ...

  2. disable_functions php-fpm root

    php.ini disable_functions 禁用某些函数 需要时注意打开 php-fpm 对应conf user group为root时 ERROR: [pool www] please sp ...

  3. [CF183D]T-shirt

    [CF183D]T-shirt 题目大意: 有\(n(n\le3000)\)个人和\(m(m\le300)\)种T恤,每个人都有一种喜欢的T恤,你知道每个人喜欢每种T恤的概率\(p_{i,j}\). ...

  4. hdu 4549 矩阵快速幂

    题意: M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F ...

  5. Java并发(十七):ConcurrentHashMap

    先做总结: 1.HashMap HashTable ConcurrentHashMap HashMap:线程不安全 HashTable:线程安全,每个方法都加了 synchronized 修饰.类似 ...

  6. 【10.5校内测试】【DP】【概率】

    转移都很明显的一道DP题.按照不优化的思路,定义状态$dp[i][j][0/1]$表示吃到第$i$天,当前胃容量为$j$,前一天吃(1)或不吃(0)时能够得到的最大价值. 因为有一个两天不吃可以复原容 ...

  7. 升级Tornado到4后weibo oauth登录不了

    把 Tornado 升级到4后,发现正常运行的微博登录不可以了. 原因是4已经移除 RequestHandler.async_callback and WebSocketHandler.async_c ...

  8. 如何添加mysql到环境变量

    环境: 在自己安装的lampp环境下,当使用mysql的时候必须指定路径才能进入数据库:这样显得太过麻烦.我们可以通过将mysql加入到环境变量中来解决该问题(mysql执行路径/opt/lampp/ ...

  9. Apache 如何反向代理tomcat并且实现Session保持

    简介 LAMT=Linux+Apache+MySQL+Tomcat: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器: 在中小型系统和并发访问用户不是很多的场合下 ...

  10. 使用Chrome快速实现数据的抓取(一)——概述

    对于一些简单的网页,我们可以非常容易的通过Develop Tool来获取其请求报文规律,并仿照其构建报文来获取页面信息.但是,随着网页越来越复杂,许多页面是由js动态渲染生成的.要获取这类信息,则需要 ...