Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Executing memoized function return function but not the result

I am trying to adapt the memoize function to the small utility method, however, I am doing something wrong and would appreciate if someone can suggest why :)

const config = {
    14340: 1195,
    15774: 1226,
    17208: 1257,
    18642: 1288,
    20076: 1319
}

const memoize = (fn) => {
    let cache = {}
    
    return (...args) => {
        let cacheKey = args.map(n => n.toString() + '+').join('')
        if (cacheKey in cache) {
            return cache[cacheKey]
        } else {
            let result = args.reduce((acc, curr) => fn(acc, curr), 0);
            cache[cacheKey] = result;
            return result;
        }
    }
}

function _getMaxValue(config) {
    const keys = Object.keys(config)
        .map((k) => parseInt(k, 10))
        .sort();
  
    return config[keys[keys.length - 1].toString()];
}

const getMaxValue = (config) => memoize(_getMaxValue)

const _add = (a, b) => {
    return a + b
}
const add = memoize(_add)

console.log(getMaxValue(config)) // memoize return block
console.log(add(1, 2)) // 3

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda