Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
Is it possible to make the key of an object more than one word?

I am trying to solve the following problem: (Not a homework question, just interview practice).

Create a function "fastCache" that takes one argument (a function) and returns a function. When fastCache is invoked it creates an object that tracks calls to the returned function, where each input to the returned function is associated with its output. Every subsequent call to that returned function with the same argument will return the output directly from the object, instead of invoking the original function again.

I was able to solve this when the input is just one argument. But for the following example, I have no idea how to go about it: L

// // MULTIPLE ARGUMENTS CASE
const sumMultiplyBy2 = (num1, num2) => 2 * (num1 + num2);
const cachedSumMultiplyBy2 = fastCacheMult(sumMultiplyBy2);
console.log(cachedSumMultiplyBy2(5, 10)); // -> 30
console.log(cachedSumMultiplyBy2(1, 2)); // -> 6
console.log(cachedSumMultiplyBy2(5, 10)); // -> 30 // from the cache object
// */

This is the code I wrote for the single argument case:

const fastCache = (callback) => {
    const obj = {};
    return (input) => {
        if (input in obj) {
            console.log("from cache");
            console.log(obj);
            return obj[input];
        }
        else {
            obj[input] = callback(input);
            return callback(input);
        }
    }
}

iplyBy2 = num => num * 2;
const cachedMultiplyBy2 = fastCache(multiplyBy2);
console.log(cachedMultiplyBy2(100)); // -> 200
console.log(cachedMultiplyBy2(150)); // -> 300
console.log(

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I guess this is what you are looking for:

function add(...numbers) {
    return numbers.reduce((accumulator, current) => {
        return accumulator += current;
    });
};



function sumMultiplyBy2(numbers){
      return add(numbers) * 2
   }
 
sumMultiplyBy2(200) //400
sumMultiplyBy2(100,50) //300
about 4 years ago · Juan Pablo Isaza Denunciar

0

So I tried joining the arguments as suggested in the comments and got a working code:

const fastCacheMult = (callback) => {
    const obj = {};
    return (...inputs) => {
        if (inputs.join('') in obj) {
            console.log("from cache");
            return obj[inputs.join('')];
        }
        obj[inputs.join('')] = callback(...inputs);
        return callback(...inputs);
    }
}


const sumMultiplyBy2 = (num1, num2) => 2 * (num1 + num2);
const cachedSumMultiplyBy2 = fastCacheMult(sumMultiplyBy2);
console.log(cachedSumMultiplyBy2(5, 10)); //30
console.log(cachedSumMultiplyBy2(1, 2)); //6
console.log(cachedSumMultiplyBy2(5, 10)); //30

However, I also found that I can just use the rest parameter and it works for both single and multiple arguments:

function fastCacheMult(callback) {
  let obj = {};
  return function (...inputs) {
    if (obj[inputs]) {
      console.log("from cache");
      return obj[inputs];
    }
    obj[inputs] = callback(...inputs);
    return obj[inputs];
  }
}


const sumMultiplyBy2 = (num1, num2) => 2 * (num1 + num2);
const cachedSumMultiplyBy2 = fastCacheMult(sumMultiplyBy2);
console.log(cachedSumMultiplyBy2(5, 10)); // -> 30
console.log(cachedSumMultiplyBy2(1, 2)); // -> 6
console.log(cachedSumMultiplyBy2(5, 10)); // -> 30 // from the cache object

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda