I have the following function. The problem I'm having is that I can't get the var out c
of the function contains
. So the console.log(c)
one I have on me if
doesn't work. How can I change contains
to make this work?
function contains(target, pattern) {
var value = 0;
pattern.forEach(function(word) {
value = value + target.includes(word);
});
var c = value;
return (value === 1)
}
if (contains(a, b)) {
console.log(a);
console.log(c);
} else {
//false statement..do nothing
}
Taking the variable out of the function:
You could just return
value
and check that:1
) it means "it contains it".Example:
You have several alternatives to be able to make use of the variable
c
. Some of them are these:Declare the variable
c
outside the context of the function, in a more global scope :Return an object with the two needed values:
Finally, simply implement it via callbacks .