PineNuts0
PineNuts0

Reputation: 5234

JavaScript: Create Function That Returns Function That Returns Either Value or Object Based on Input Param

I want to:

Create a function saveOutput that accepts a function (that will accept one argument), and a string (that will act as a password). saveOutput will then return a function that behaves exactly like the passed-in function, except for when the password string is passed in as an argument. When this happens, the returned function will return an object with all previously passed-in arguments as keys, and the corresponding outputs as values

I tried the following code below:

const saveOutput = (inputFunc, str) => {

  let newObj = {}; 

  return function (value) {

    if (value === str){

      return newObj[value] = inputFunc(value)
    }
    // return a function that behaves exactly like the passed-in function
    else {
      return inputFunc(value)
    }
  }
}

// Uncomment these to check your work!
const multiplyBy2 = function(num) { return num * 2; };

const multBy2AndLog = saveOutput(multiplyBy2, 'boo');

console.log(multBy2AndLog(2)); // should log: 4
console.log(multBy2AndLog(9)); // should log: 18
console.log(multBy2AndLog('boo')); // should log: { 2: 4, 9: 18 }

My code returns:

console.log(multBy2AndLog(2)); // returns 4
    console.log(multBy2AndLog(9)); // returns 18
    console.log(multBy2AndLog('boo')); // returns NaN

Why does my third and final console.log return NaN whe it should return:

{ 2: 4, 9: 18 }

Upvotes: 1

Views: 953

Answers (2)

MedAli Khaled
MedAli Khaled

Reputation: 11

function saveOutput(func, magicWord) {
  let obj = {};
  function newFun(x) {
    if(x != magicWord) {
      obj[x] = func(x);
      return func(x)
    } else {
      return obj;
    }
  }
  return newFun;
}



const multiplyBy2 = function(num) { return num * 2; };
const multBy2AndLog = saveOutput(multiplyBy2,'boo');
console.log(multBy2AndLog(2)); // => should log 4
console.log(multBy2AndLog(9)); // => should log 18
console.log(multBy2AndLog('boo')); // => should log { 2: 4, 9: 18 }

Upvotes: 0

Fraction
Fraction

Reputation: 12984

You must move the newObj assignment to the else clause and return newObj when value === str:

if (value === str){     
   return newObj;
}
// return a function that behaves exactly like the passed-in function
else {
   newObj[value] = inputFunc(value);
   return inputFunc(value);
}

Live example:

const saveOutput = (inputFunc, str) => {

  let newObj = {}; 

  return function (value) {

    if (value === str){     
      return newObj;
    }
    // return a function that behaves exactly like the passed-in function
    else {
      newObj[value] = inputFunc(value);
      return inputFunc(value)
    }
  }
}

// Uncomment these to check your work!
const multiplyBy2 = function(num) { return num * 2; };

const multBy2AndLog = saveOutput(multiplyBy2, 'boo');

console.log(multBy2AndLog(2)); // should log: 4
console.log(multBy2AndLog(9)); // should log: 18
console.log(multBy2AndLog('boo')); // should log: { 2: 4, 9: 18 }

Upvotes: 1

Related Questions