0
I am a beginner in Node.js and I created an API that validates a CPF and returns a value true or false, but I am not able to send the return of this function to my other application. Follows code:
export default function discourseHandler(r) {
if (r.context.cpf){
function is_cpf (c) {
if((c = String(c).replace(/[^\d]/g,"")).length != 11)
return false;
if (c == "00000000000" ||
c == "11111111111" ||
c == "22222222222" ||
c == "33333333333" ||
c == "44444444444" ||
c == "55555555555" ||
c == "66666666666" ||
c == "77777777777" ||
c == "88888888888" ||
c == "99999999999")
return false;
var r;
var s = 0;
var i;
for (i=1; i<=9; i++)
s = s + parseInt(c[i-1]) * (11 - i);
r = (s * 10) % 11;
if ((r == 10) || (r == 11))
r = 0;
if (r != parseInt(c[9]))
return false;
s = 0;
for (i = 1; i <= 10; i++)
s = s + parseInt(c[i-1]) * (12 - i);
r = (s * 10) % 11;
if ((r == 10) || (r == 11))
r = 0;
if (r != parseInt(c[10]))
return false;
return true;
}
is_cpf (r.context.cpf);
r.context.cpf = is_cpf();
}
return r;
}
Could you put where exactly this function should be included to help answer?
– mutlei
The application that will receive the return of the function is a Watson Assistant bot that is encoded in JSON. The variable that sends the Watson CPF to the function is "r.context.Cpf". So I don’t know if you have to use the same variable to send the function result or you have to create another one.
– c-oliveira
Like, in Watson, you call the function that way:
valida_cpf(CPF_VAR)
or soresultado = valida_cpf(CPF_VAR)
? The first mode, the type of return is not important, in the second it is already.– mutlei
I call it is_cpf (r.context.Cpf);
– c-oliveira
If that function
is_cpf
has some kind of return, you will have to assign to a variable the return if you want to use it, likeO CPF <valor enviado> é válido
. Otherwise, you don’t need.– mutlei
Ok, I’ll assign the return to a variable and test it here. Thank you!
– c-oliveira