0

This is my first question here, I have tried to return callback before my if statement, but still doesn't work, if I get the actual number I can not get the string, I have put more than 20 hr in this one, any help will be really appreciated

function stored(callback) {
  return function(n) {
    if (typeof n === 'string') {
      return 'Please enter a valid number'
    }
    return `${n}: ${callback(n)}`;
    return callback(n);
  }
}


const cube = (n) => n ** 3;
const cubeStored = stored(cube);
cubeStored(2) //--> 8
cubeStored(2) //--> '2: 8'
cubeStored(3) //--> 27
cubeStored(3) //--> '3: 27'
cubeStored('a') //--> "Please enter a valid number"
cubeStored('a') //--> "Please enter a valid number"
cubeStored(2) //--> '2: 8'
6
  • you are returning from function before calling your callback, hence it is not getting exceuted. Commented Sep 24, 2020 at 4:10
  • I'm still lost, should I create a variable between function store and the return it one? Commented Sep 24, 2020 at 4:28
  • Can you elaborate as to why exactly you have two return statements one after the other? The second will never be executed. Commented Sep 24, 2020 at 4:57
  • please usetypeof n !== 'number', not typeof n === 'string' for that check Commented Sep 24, 2020 at 6:25
  • @esqew. I guess my interpretation on the instruction wasn't the best, and that's why, I wasn't thinking that if I return, there will be nothing else to call in the call stack , Thanks Commented Sep 25, 2020 at 17:13

2 Answers 2

1

You need to remember the arguments that you're passing into your function such that when you call your function again with the same arguments it returns a string, otherwise, if you call it with an argument that you haven't used before it returns a number.

This can be achieved by building an object, which your inner function closes over. The object will store keys which are based on the arguments passed into the function. The value of each key will be the result of calling the function with the arguments stored at the key.

When you call your function with arguments, you can check to see if the object you made has the arguments you passed in. If so, you can return the value stored at the arguments key of your object. Otherwise, if the arguments aren't a key in your object, you can set the key and the value, and return the value of the object you just set.

See code comments for further details:

function stored(callback) {
  const memo = {}; // used to store already seen arguments and their respective return values
  return function(n) {
    if (typeof n === 'string') {
      return 'Please enter a valid number'
    } else if(n in memo) { // if `n` has already been seen, grab the "remembered" result.
      return `${n}: ${memo[n]}`;
    } else { // if function hasn't been called with `n` before, then save the result
      memo[n] = callback(n);
      return memo[n];
    }
  }
}

const cube = (n) => n ** 3;
const cubeStored = stored(cube);
console.log(cubeStored(2)) //--> 8
console.log(cubeStored(2)) //--> '2: 8'
console.log(cubeStored(3)) //--> 27
console.log(cubeStored(3)) //--> '3: 27'
console.log(cubeStored('a')) //--> "Please enter a valid number"
console.log(cubeStored('a')) //--> "Please enter a valid number"
console.log(cubeStored(2)) //--> '2: 8'

The above can be generalized a little more to handle n arguments by using the rest parameter syntax (...):

function stored(callback) {
  const memo = {};
  return function(...args) {
    const key = JSON.stringify(args);
    if (args.length <= 0 || typeof args[0] === 'string') {
      return 'Please enter a valid number'
    } else if(key in memo) {
      return `${key}: ${memo[key]}`;
    } else { // if function hasn't been called with `n` before, then save the result
      memo[key] = callback(...args);
      return memo[key];
    }
  }
}


const cube = (n) => n ** 3;
const cubeStored = stored(cube);
console.log(cubeStored(2)) //--> 8
console.log(cubeStored(2)) //--> '2: 8'
console.log(cubeStored(3)) //--> 27
console.log(cubeStored(3)) //--> '3: 27'
console.log(cubeStored('a')) //--> "Please enter a valid number"
console.log(cubeStored('a')) //--> "Please enter a valid number"
console.log(cubeStored(2)) //--> '2: 8'

Sign up to request clarification or add additional context in comments.

Comments

0

If you have multiple return from function you can use generator function of ES6. Generator function yield result and we can access the same value with next()

function stored(callback) {
  return function*(n) {
    if (typeof n === 'string') {
      yield 'Please enter a valid number'
    } else {
      yield callback(n);
      yield `${n}: ${callback(n)}`
    }
  }

}
const cube = (n) => n ** 3;
const cubeStored = stored(cube);
const c = cubeStored(2);
console.log(c.next().value)
console.log(c.next().value)
const d = cubeStored(3);
console.log(d.next().value)
console.log(d.next().value)
const e = cubeStored('3');
console.log(e.next().value)

1 Comment

I read about generator function, but didn't know how to use it , I like what you did. I 'm going to dive in to generator function, Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.