-2

I'm trying to assign this function to this variable but no matter what i try nothing works and it gives me this error TypeError: addSix is not a function

 const createBase = num => {
      num + 6
    }
    
    let addSix = createBase(6);
    addSix(10); // returns 16
    addSix(21); // returns 27

Any suggestions please?

Marios
  • 26,333
  • 8
  • 32
  • 52
Roffy bc
  • 91
  • 1
  • 8

5 Answers5

4

For addSize to be a function, the return value of createBase must be a function.

createBase doesn't return anything. It has no return statement. Consequently, it returns undefined.

It doesn't even create a function to return.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I added return statement but still got the same error – Roffy bc Oct 30 '20 at 08:47
  • 1
    @Roffybc — I'm guessing you put the return statement at the start of the only line in the function: So now you are returning a number. A number isn't a function. – Quentin Oct 30 '20 at 08:48
4

There are a couple of different problems:

  1. createBase is a function that doesn't return anything.

  2. The part that you're trying to create as a function (num => { num + 6 }) should either not have {} or should have a return (more here).

  3. You're hardcoding the 6, but you should be using the parameter value passed to createBase.

You need to make createBase a function that returns a function:

// `createdBase`   vvvvvvvvvvvvvvvvvvvvvvvvvvv
const createBase = num1 => num2 => num1 + num2;
// The func it returns     ^^^^^^^^^^^^^^^^^^^

let addSix = createBase(6);
console.log(addSix(10)); // returns 16
console.log(addSix(21)); // returns 27
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

First things first; num => { num + 6 }. If you're using curlies, you have to add the return statement. Otherwise, lose the curlies: num => num + 6. This will implicitly add a return statement.

Then, If you're not used to working with closures, I advice you not to use arrow functions.

And while this may work

const createBase = (num) => (num) => num + 6;
let addSix = createBase(6);
console.log(addSix(10));
console.log(addSix(21));

It's far more clear what it does for javascript beginners when you type it out like this;

const createBase = function(num) {
   return function (num) {
     return num + 6;
   }
};
let addSix = createBase(6);
console.log(addSix(10));
console.log(addSix(21));

What you had was the following, which if written out, makes it clear addSix was assigned the value of num+6 instead of being a function.

const createBase = function (num) {
    return num + 6
};
Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49
1

There are two issues:

Issue 1:

You need to use a return statement or remove the curly brackets:

const createBase = num => num + 6;

Issue 2:

createBase(6) returns a number not a function.

Therefore you can't pass arguments to a number.


Example:

const createBase = num => num + 6;
let addSix = createBase(6); // this is a number, not a function
addSix(21); // you can't call a number with arguments as function and hence the error

addSix is a number, you can't pass arguments to it and use it as a function because it is not.

Marios
  • 26,333
  • 8
  • 32
  • 52
0

It seems like you want to create a single argument fucntion that returns a single argument function, which could be achieved like this.

const createBase = base => (num) => base + num;
let addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27

Also see:

  • Partial application
  • Currying

(They're not quite the same: What is the difference between currying and partial application?)

Will
  • 2,858
  • 6
  • 33
  • 50