sahil sahil - 18 days ago 7
Javascript Question

Return an Boolean array by matching words in two arrays

The function

keywordusage
should return which of the keywords are present in the text in the form of boolean array like:
array: [true, false, false]
. based on the second argument to the function. Right now, I am able to return only the
true
values, I do not know how to return
false
values when the case is false. The code is below. The output should be [true, false, false]

{function keywordusage(str,strArr){
var count = [];
var found = true;
var notfound = false;
var words = str.split(' ');
var wordsLength = words.length;
var ArrLength = strArr.length;
for(var i = 0; i < ArrLength; i++){
for(var j = 0; j < wordsLength; j++){
if(words[j]===strArr[i])
count.push(true);
}
}
return count;
}

document.write(keywordusage('Dive Into Python is a free book for experienced programmers', ['Python', 'python', 'scala']));}

Answer
function keywordusage(text, keywords) {
    var elements = text.split(" ");
    var result = Array();
    for (var i=0; i < keywords.length;i++){
        if (elements.indexOf(keywords[i]) >= 0)
            result[i] = true;
        else
            result[i] = false;
    }//end of for loop
    return result;
}

var result = keywordusage('Dive Into Python is a free book for experienced programmers', ['Python', 'python', 'scala']);
console.log(result)