Special Multiply

Special Multiply

Special Multiply is a form of code where we have one function and we check if they are not arrays, strings or objects plain numbers and we return a callback function where we multiply the parameters;

Example:
console.log(specialMultiply(3,2)) // 6
console.log(specialMultiply(5,-2))// -10

function specialMultiply(a,b){
  if(arguments.length === 1){
    return function(b){
      return a*b;
    }
  }
  return a*b;
}
console.log(specialMultiply(3,2)) // 6
console.log(specialMultiply(5,-2))// -10

We create a function called specialMultiply with parameter called a and b

We check if (arguments.length ===1) {}

If true we return another function with parameter b and return ab

In the end we return again a
b

comments powered by Disqus