OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why is my function returning undefined when using another function as a parameter?

  • Thread starter Thread starter antjuh
  • Start date Start date
A

antjuh

Guest
The problem I am having is with the following code:

Code:
/**
 * @param {(sentence: string) => boolean} criterion - a function that
 *  takes a sentence and returns a boolean
 * @param {string[]} sentences - an array of space-separated strings of words
 * @returns {string[]} the subset of `sentences` for which `criterion` returns true
 */
const getRelevantSentences = (about, sentences) => {
    const arr = [];
      if(about(sentences) == true){
        arr.push(sentences);
        return arr;
      }
}

This above function is supposed to take another function and an array as arguments, and then pass out an array of strings that make the function return true. In this instance, I am trying to test whether or not the specific "sentence" string within the "sentences" array makes the about() function return true. In other words, I am returning any strings that are "similar" to that of another string.

Below is the code that makes up the about() function:

Code:
/**
 * @param {string[]} topics - an array of topic words
 * @param {string} sentence - a space-separated string of words
 * @returns {boolean} whether `sentence` contains any of the words in `topics`
 */
const isRelevant = (topics, sentence) => {
  for(i=0; i<topics.length;i++){
    if (sentence.includes(topics[i])) {
      return true;
    }
    else {
      return false;
    }
  }
};

/**
 * @param {string[]} topics - an array of topic words
 * @returns {(sentence: string) => boolean} a function that takes a sentence
 *  and returns whether it is relevant to `topics`
 */
const about = (topics) => {
    return function(sentence) {
      return isRelevant(topics,sentence);
    }
};

The data used as input is:

const sentence = "the quick brown fox jumps over the lazy dog"

const sentences = "I have a pet dog", "I have a pet cat", "I don't have any pets"

How can I resolve this issue?

<p>The problem I am having is with the following code:</p>
<pre><code>/**
* @param {(sentence: string) => boolean} criterion - a function that
* takes a sentence and returns a boolean
* @param {string[]} sentences - an array of space-separated strings of words
* @returns {string[]} the subset of `sentences` for which `criterion` returns true
*/
const getRelevantSentences = (about, sentences) => {
const arr = [];
if(about(sentences) == true){
arr.push(sentences);
return arr;
}
}
</code></pre>
<p>This above function is supposed to take another function and an array as arguments, and then pass out an array of strings that make the function return true. In this instance, I am trying to test whether or not the specific "sentence" string within the "sentences" array makes the <code>about()</code> function return true. In other words, I am returning any strings that are "similar" to that of another string.</p>
<p>Below is the code that makes up the <code>about()</code> function:</p>
<pre><code>/**
* @param {string[]} topics - an array of topic words
* @param {string} sentence - a space-separated string of words
* @returns {boolean} whether `sentence` contains any of the words in `topics`
*/
const isRelevant = (topics, sentence) => {
for(i=0; i<topics.length;i++){
if (sentence.includes(topics)) {
return true;
}
else {
return false;
}
}
};

/**
* @param {string[]} topics - an array of topic words
* @returns {(sentence: string) => boolean} a function that takes a sentence
* and returns whether it is relevant to `topics`
*/
const about = (topics) => {
return function(sentence) {
return isRelevant(topics,sentence);
}
};
</code></pre>
<p>The data used as input is:</p>
<p><code>const sentence = "the quick brown fox jumps over the lazy dog"</code></p>
<p><code>const sentences = "I have a pet dog", "I have a pet cat", "I don't have any pets"</code></p>
<p>How can I resolve this issue?</p>
 

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top