JS代码无法解决
JS code cannot solve
写一个函数,取一个数字n和一个函数f,并返回一个函数g。
当您调用g((时,它最多调用f((n次。
前任。
function log() {
console.log('called log');
}
var onlyLog = only(3, log);
onlyLog(); -> outputs 'called log' to console
onlyLog(); -> outputs 'called log' to console
onlyLog(); -> outputs 'called log' to console
onlyLog(); -> does nothing
onlyLog(); -> does nothing
我下面的代码:
toolbox.only = function(n, f) {
for (var i = 0; i <= n; i++) {
var called = false;
return function() {
if (!called) {
f();
called = true;
}
}
}
}
我的代码没有通过以下测试:只调用(3,f(超过3次应该调用f((3次。
如有任何帮助,我们将不胜感激,并提前表示感谢。
我觉得你想得太多了。。
请尝试下面的操作。。
// Write a function that takes a number n and a function f, and returns a function g.
// When you call g() it calls f() at most n times.
// ex.
// function log() {
// console.log('called log');
// }
// var onlyLog = only(3, log);
// onlyLog(); -> outputs 'called log' to console
// onlyLog(); -> outputs 'called log' to console
// onlyLog(); -> outputs 'called log' to console
// onlyLog(); -> does nothing
// onlyLog(); -> does nothing
var only = function(n, f) {
return function () {
if (n) {
n --;
f();
}
}
}
function log() {
console.log('called log');
}
var onlyLog = only(3, log);
onlyLog();// -> outputs 'called log' to console
onlyLog();// -> outputs 'called log' to console
onlyLog();// -> outputs 'called log' to console
onlyLog();// -> does nothing
onlyLog();// -> does nothing
相关文章:
- 有人能帮我理解为什么这个JavaScript代码赢得'不起作用,以及我如何解决它
- node.js nodeschool learnyounode需要帮助理解解决方案的代码
- 小型JavaScript解决方案需要:此行是否有效代码
- 了解两个代码解决方案之间的结果差异
- 如何为此 jquery 代码使用函数参数,或者是否有更好的解决方案
- Jquery代码要解决的问题
- 为什么预期值仅在我在调试模式下运行代码时出现,但在不调试时中断,以及如何解决此问题
- 我如何解决“;这个“;我的代码中的对象混淆
- 使用Jquery解决所有承诺后执行代码
- 单元测试Mootools代码的最佳解决方案
- 为什么这个解决n皇后谜题的代码不能在javascript中工作?
- jQuery删除重复代码-更简单的解决方案
- JS代码无法解决
- 如何修改给定的代码打开两个链接在一次点击(没有解决方案为我工作)
- 是否可以编写Javascript代码来解决浏览器兼容性问题?
- 此代码不起作用,我该如何解决
- 有人能帮我解决我的代码笔脚本获胜的原因吗;t在文本编辑器中运行
- 我的Vue.js代码中有很多重复,我该怎么解决呢
- 为什么在以下代码中出现循环引用错误?以及如何解决它
- 灯箱类型的解决方案,以放大代码