0

Here's what I have:

function verificarNumero(test, num) {
    return (test(num));
};

var resultadoTesteMultiplos = verificarNumero(function (num){return (num % 10 == 0);}, num1);

This function is supposed to find out if a number is a multiple of 10. I know there are simpler ways to do it, but I really want to make this work.

I want to be able to do something like console.log(resultadoTesteMultiplos(10)); but the console returns "resultadoTesteMultiplos is not a function", and "num1 is undefined."

What am I doing wrong?

10
  • 3
    Ok. Where do you define num1? Commented Mar 21, 2017 at 9:51
  • I'm passing it as an argument, so I don't need to define it... right? Commented Mar 21, 2017 at 9:53
  • 2
    What you need is a function returning a function. Commented Mar 21, 2017 at 9:53
  • @IgorSílva — Only if you are happy with the value being undefined when the function you are passing it to receives it (and you haven't declared it either, so you'll get a ReferenceError for that). Commented Mar 21, 2017 at 9:54
  • Why do you want to wrap a function you created im a function? Commented Mar 21, 2017 at 9:55

3 Answers 3

4

Sounds like you meant to curry but only got half way:

function verificarNumero(test) {
  return function(num) {
    return test(num);
  };
}

var resultadoTesteMultiplos = verificarNumero(function(num) {
  return (num % 10 == 0);
});

console.log(resultadoTesteMultiplos(10));
Sign up to request clarification or add additional context in comments.

Comments

2

If you define num1 variable your code should work and the type of the resultadoTesteMultiplos is boolean. See the working snippet below please:

var num1 = 10;
function verificarNumero(test, num) {
  return (test(num));
};

var resultadoTesteMultiplos = verificarNumero(function(num) {
  return (num % 10 == 0);
}, num1);
console.log(typeof resultadoTesteMultiplos);
console.log(resultadoTesteMultiplos);

2 Comments

Yes i tried that, what i wanted was to pass a value to num1 without having to set it like that.
@IgorSílva, ok, I understand. The above answer should work too, but I see the difference between mine an the accepted answer and I'm happy you found what you needed.
0

Return a function from verificarNumero, not the result of the function.

function verificarNumero (cb) {
    return function (num) {
        return cb.apply(this, [num]);
    }    
};

resultadoTesteMultiplos = verificarNumero(function (num) {
    return (num % 10 == 0);
};

console.log(resultadoTesteMultiplos(10));

Comments

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.