How to return the function value in node.js?

File index.js
var mymodule = require('./mymodule');
var testword = require('./testword.json');

for( var word in testword){
 var result = testword[word];
 console.log('word: ', word, 'result: ', result);
}


File mymodule.js
module.exports.test = function (word) {
if(word.indexOf('a')+1){
 return true;
}
else{
 return false;
}
};


File testword.json
{
 "yandex": true,
 "google": true,
 "huyandex": false
}


The test function in mymodule.js checks a word and returns true or false
Question: how can I compare her answer with true or false which are defined in the document testword.json ?
July 12th 19 at 16:52
1 answer
July 12th 19 at 16:54
Solution
Not very clear what you do, but apparently so:
var tester = require('./mymodule').test
 assertions = require('./testword.json');

for (var word in testword) {
 var expected = assertions[word],
 actual = tester(word);

 console.assert(actual === expected, actual, expected);
}


But your verification word.indexOf('a')+1 will be held for any word that has a letter "a". And this letter is to both check words, so that test is obviously wrong.
The test sample that would be a lot of code on toster not to put
Put your code in index.js but says that mymodule is not a function
1brCOzQHqfw.jpg - josue_Thiel commented on July 12th 19 at 16:57
Well, you yourself wrote the code, this error could be corrected. Of course, it should be mymodule.test(word) - parker commented on July 12th 19 at 17:00
: Yes, I understand your frustration )) Just if in JS I understand the nod, especially with the modular system is weak. Now for example error:
QskWypJcWwg.jpg
And added the word google in which there is no letter "a" ;) - josue_Thiel commented on July 12th 19 at 17:03
Well, this is a normal error, it console.assert throws, because you have "huyandex": false, and mymodule.test('huyandex') returns true, due to the particular validation logic, which I described. - parker commented on July 12th 19 at 17:06
I reveal a terrible secret - I'm under the gcd in life wrote 6 hours in total. No special knowledge for the solution of your problem is needed - just a basic knowledge of js and attentiveness. - parker commented on July 12th 19 at 17:09
All understood, no I hardcore tested didn't replaced the console.assert on:
console.log('word: ', word,'expected: ', expected, 'actual: ', actual);

and everything worked as it should. Thank you! - josue_Thiel commented on July 12th 19 at 17:12

Find more questions by tags Node.jsJavaScript