U
U
Uber Noob2016-04-30 07:00:39
JavaScript
Uber Noob, 2016-04-30 07:00:39

How to return a function value in node.js?

index.js file

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;
  }
};

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

The test function in mymodule.js tests a word and returns true or false
Q: How can I compare its response to true or false already set in the testword.json document?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-04-30
@ubernoob

It's not really clear what you're doing at all, but probably something like this:

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 check word.indexOf('a')+1will pass for any word that has an "a" in it. And there is such a letter in both tested words, so the test is obviously wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question