【问题标题】:Bootcamp Entrance Exam Question - CheckerLogger Closure Problem in Javascript训练营入学考试题 - Javascript 中的 CheckerLogger 闭包问题
【发布时间】:2021-10-19 01:41:29
【问题描述】:

这是来自现场训练营的考试题。我碰壁了,无法弄清楚为什么代码无法正确执行。我做错了什么?

这是提供的提示:

创建一个接受一个参数的函数checkerLogger(一个返回布尔值的函数)返回的函数应该具有以下行为: 如果使用参数调用该函数,则调用检查器回调函数并返回其布尔结果。 如果函数在没有任何参数的情况下被调用,则返回回调函数被调用的次数并计算为真或假。

例子:你应该得到什么

const isOdd = num => num % 2 === 1;

const oddCounter = checkerLogger(isOdd);

console.log(oddCounter()); //-> { true: 0, false: 0 }

console.log(oddCounter(3)); //-> true

console.log(oddCounter(2)); //-> false

console.log(oddCounter()); //-> { true: 1, false: 1 }

我的代码:

function checkerLogger (cb) {
  const cache = {};
  return function (arg){
    let result = cb(arg)
    if (arg){
      return result;
    }

    if (cache.hasOwnProperty(result)){
      cache[result] += 1;
    } else {
      cache[result] = 0
    }

    return cache;
  }
}

const isOdd = num => num % 2 === 1;

const oddCounter = checkerLogger(isOdd);

console.log(oddCounter()); //-> { true: 0, false: 0 }

console.log(oddCounter(3)); //-> true

console.log(oddCounter(2)); //-> false

console.log(oddCounter()); //-> { true: 1, false: 1 }

我的输出:

{ false: 0 } 

true 

false 

{ false: 1 } 

【问题讨论】:

    标签: javascript closures javascript-objects


    【解决方案1】:

    您可以将cache 对象初始化为:

    const cache = { true: 0, false: 0 }
    

    这将帮助您避免检查truefalse 属性是否存在于cache 对象中。

    在返回的函数内部,首先检查它是否在没有参数的情况下被调用。如果未提供参数,则只需返回 cache 对象。

    if (arg == undefined) {
       return cache;
    }
    

    如果已提供参数,则调用回调函数,然后更新cache 对象。

    let result = cb(arg);
    cache[result] += 1; 
    

    最后,返回result

    演示

    function checkerLogger(cb) {
    
      const cache = { true: 0, false: 0 };
    
      return function(arg) {
        if (arg == undefined) {
          return cache;
        }
        
        let result = cb(arg);
        cache[result] += 1;  
        
        return result;
      }
    }
    
    const isOdd = (num) => num % 2 === 1;
    const oddCounter = checkerLogger(isOdd);
    
    console.log(oddCounter());
    console.log(oddCounter(2));
    console.log(oddCounter(2));
    console.log(oddCounter(3));
    console.log(oddCounter());

    编辑

    根据您的评论,您不想使用包含 truefalse 属性的对象来初始化 cache 常量。

    以下演示展示了如何做到这一点,并且仍然获得相同的输出。

    function checkerLogger(cb) {
    
      const cache = {};
    
      return function(arg) {
        if (arg == undefined) {
          return { true: cache['true'] ?? 0, false: cache['false'] ?? 0 };
        }
        
        let result = cb(arg);
     
        if (cache[result] != undefined) {
          cache[result] += 1;
        } else {
          cache[result] = 1;
        }
        
        return result;
      }
    }
    
    const isOdd = (num) => num % 2 === 1;
    const oddCounter = checkerLogger(isOdd);
    
    console.log(oddCounter());
    console.log(oddCounter(2));
    console.log(oddCounter(2));
    console.log(oddCounter(3));
    console.log(oddCounter());

    【讨论】:

    • 哇,谢谢,这几乎就是我想要的。通过将最后一个“返回缓存”更改为“返回结果” - 我确实得到了我需要的结果。但是有没有一种方法可以在不声明对象的真假的情况下得到答案。我想获得动态声明键的函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2021-07-29
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多