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. 【推导】Codeforces Round #478 (Div. 2) D. Ghosts

    题意:给你一条直线以及初始时刻这条直线上的一些人的坐标,以及他们的速度矢量.让你对每个人计算他在过去无限远到将来无限远的时间内会与多少人处于同一个点,然后对每个人的这个值求和. 列方程组:两个人i,j ...

  2. 【漏洞预警】方程式又一波大规模 0day 攻击泄漏,微软这次要血崩

    一大早起床是不是觉得阳光明媚岁月静好?然而网络空间刚刚诞生了一波核弹级爆炸!Shadow Brokers再次泄露出一份震惊世界的机密文档,其中包含了多个精美的 Windows 远程漏洞利用工具,可以覆 ...

  3. hdu 4544 优先队列+贪心

    题意:最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏.游戏规则很简单,用箭杀死免子即可.箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di ...

  4. 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2107  Solved: 820[Submi ...

  5. BZOJ 4602: [Sdoi2016]齿轮 dfs

    4602: [Sdoi2016]齿轮 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4602 Description 现有一个传动系统,包 ...

  6. 重温PHP之插入排序

    插入排序基本思路:将数组分为两个区(已排序区和未排序区),假定数组的第一个元素处于已排序区, 第一个元素之后的所有元素都处于未排序部分.排序时用到双层循环,外层循环用于从未排序部分中取出待排序元素,并 ...

  7. lamp经典安装

    一.网络方面的知识 2 ①-网络常见的命令 2 ②-网卡相关 2 ③-防火墙相关 2 ④-selinux相关 3 二.上传amp源代码包 5 三.linux下软件安装-vsftpd安装 6 ①-rpm ...

  8. Knockout官网实例在MVC下的实现-01,实现Hello world

    本篇使用Knockout在MVC下实现"Hello World",对应的官网实例在这里. View视图 Knockout的一个特点是:声明式绑定,即Declarative bind ...

  9. java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    一:如果出现下面的错误信息,如果你的项目是Maven结构的,那么一般都是你的项目的Maven Dependencies没有添加到项目的编译路径下: 信息: The APR based Apache T ...

  10. Matlab的linprog解决简单线性规划问题

    一个简单的线性规划问题,使用Matlab的linprog解决 假定有n种煤,各种煤的配比为x1,x2,x3,……首先需要满足下列两个约束条件,即 x1+x2+x3……+xn=1 x1≥0, x2≥0, ...