Below is the function that I am trying to create:
/**
* @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) => {
if(about(sentences)){
return [sentences];
}
else {
return [];
}
}
Where about()
is derived from the following 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;
}
}
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);
}
};
And any data needed 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"];
The desired effect for getRelevantSentences()
is that it takes in a function and an array, and returns an array that makes the function that was passed in true. In other words, I am trying to return a subset of sentences
that make the boolean (the about()
function) true.
I know the answer is something simple. I feel like I should not be using the about()
function and instead should be using the function within it, but I don’t know how I would call that.
Any help is greatly appreciated. I apologize if I overcomplicated anything.