Reputation: 75
I wrote the following code, while it works, it is very inefficient and would be open to critique/rework. Main problem is it's a nightmare to scale.
function wordCount(layers) {
var countNum = '3'
if (layers.includes('Text1') && layers.includes('Text2') && layers.includes('Text3,')) {
return countNum - 3;
} else if (layers.includes('Text1') && layers.includes('Text2')) {
return countNum - 2;
} else if (layers.includes('Text1') && layers.includes('Text3,')) {
return countNum - 2;
} else if (layers.includes('Text2') && layers.includes('Text3,')) {
return countNum - 2;
} else if (layers.includes('Text1') || layers.includes('Text2') || layers.includes('Text3,')) {
return countNum - 1;
}
return countNum;
}
console.log(wordCount('Layer 1, Text1')); //return 2
Below is what i've tried so far, but haven't been able to figure out how to count the numbers of words from an array found in a string.
function wordCount() {
let keywords = ['Text1', 'Text2', 'Text3', 'Text4', 'Text5']
let textString = 'Layer1, Text1, Text5'
let count = 0
for (var i = 0; i < keywords.length; i++) {
console.log(keywords[i].length); // when running code as-is return 5.
return textString.split(", ").length;
}
}
console.log(wordCount());; // when running code as-is return 3.
//I'm expecting the return to be 2.
Any help is greatly appreciated! Thank you in advance!
Upvotes: 0
Views: 114
Reputation: 191976
You can use Array.reduce()
with Array.includes()
to get the number. Since the list of words might change, you can create a function with partial application, that accepts a list of words, and returns and a function that you can use with the text:
const wordCount = (words) => (layers) =>
words.reduce((c, w) =>
layers.includes(w) ? c + 1 : c,
0);
const count = wordCount(['Text1', 'Text2', 'Text3']);
const result = count('Layer 1, Text1');
console.log(result) // 1
A Nashorn compatible version:
function wordCount(words) {
var found = 0;
return function(layers) {
for(var i = 0; i < words.length; i++) {
if(layers.includes(words[i])) found += 1;
}
return found;
}
}
var count = wordCount(['Text1', 'Text2', 'Text3']);
var result = count('Layer 1, Text1');
console.log(result) // 1
Upvotes: 1
Reputation: 780818
Loop through the keywords, and if it matches layers
increment a counter.
function wordCount(textString) {
let keywords = ['Text1', 'Text2', 'Text3', 'Text4', 'Text5'];
let count = 0;
keywords.forEach(function(keyword) {
if (textString.includes(keyword)) {
count++;
}
});
return count;
}
console.log(wordCount('Layer 1, Text1'));
Upvotes: 1
Reputation: 12900
Why not just keep your key words in a static array and interrogate any given string for a match count? There's still a little work to do here since it would need to be smart enough to handle punctuation. A regex could handle that part pretty easily.
const getWordCount = (phrase, keywords) => {
if (phrase) {
phrase = phrase.toLowerCase();
const phraseArray = phrase.split(' ');
let count = 0;
phraseArray.forEach( (word) => {
if ( keywords.find( k => k.toLowerCase() === word) ) {
return count += 1;
}
});
return count;
}
return 0;
};
const str = 'This is my sentence. There are many like it, but this one is mine';
const count = getWordCount(str, ['like', 'are', 'this']);
console.log(count);
Upvotes: 0