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. 数据准备<5>:变量筛选-实战篇

    在上一篇文章<数据准备<4>:变量筛选-理论篇>中,我们介绍了变量筛选的三种方法:基于经验的方法.基于统计的方法和基于机器学习的方法,本文将介绍后两种方法在Python(skl ...

  2. BZOJ.1032.[JSOI2007]祖码(区间DP)

    题目链接 BZOJ 洛谷 AC代码: 区间DP,f[i][j]表示消掉i~j需要的最少珠子数. 先把相邻的相同颜色的珠子合并起来. 枚举方法一样,处理一下端点可以碰撞消除的情况就行. 当然合并会出现问 ...

  3. CSS实现背景透明,文字不透明

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. py脚本打包exe可执行文件

    python3以上版本打包exe需要扩展软件:cx_freeze 下载地址:http://cx-freeze.sourceforge.net/ 1)安装后在\Python32\Scripts\cxfr ...

  5. MySQL: 查看一次SQL的执行时间都花在哪些环节上

    select @@profiling -- 看看当前的session的profiling打开没有 set profiling = 1 -- 如果没打开,打开一下 -- 执行一些sql select c ...

  6. linux系统时间同步,硬件时钟和系统时间同步,时区的设置

           1.时间同步(手动): date -s "2015-07-15 22:13:30" hwclock --systohc   (表示系统时间同步到硬件时钟) hwclo ...

  7. 如何在windows server 2008上配置NLB群集

    参考:http://zlwdouhao.blog.51cto.com/731028/781828 前些天写了一篇关于NLB群集模式多播和单播的简单介绍.那么下面我们一起来探讨一下,如何在windows ...

  8. panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

    go的结构体中私有的属性, 即使反射也获取不到

  9. 用最简单的例子理解装饰器模式(Decorator Pattern)

    假设有一个公司要做产品套餐,即把不同的产品组合在一起,不同的组合对应不同的价格.最终呈现出来的效果是:把产品组合的所有元素呈现出来,并显示该组合的价格. 每个产品都有名称和价格,首先设计一个关于产品的 ...

  10. MVC批量更新,使用jQuery Template

    在"MVC批量更新,可验证并解决集合元素不连续控制器接收不完全的问题"中,当点击"添加"按钮的时候,通过部分视图,在界面上添加新行.本篇体验使用jQuery T ...