Assuming I have an array of words and a few camelCase strings as follows:
var arr = ["hello", "have", "a", "good", "day", "stackoverflow"];
var str1 = "whenTheDayAndNightCollides";
var str2 = "HaveAGoodDay";
var str3 = "itIsAwfullyColdDayToday";
var str4 = "HelloStackoverflow";
How would I split the camelCase
words into individual strings, compare each split string (converted to lowercase) to the arr
array elements and return true
if every split string is part of the specified array?
"whenTheDayAndNightCollides" // should return false since only the word "day" is in the array
"HaveAGoodDay" // should return true since all the words "Have", "A", "Good", "Day" are in the array
"itIsAwfullyColdDayToday" // should return false since only the word "day" is in the array
"HelloStackoverflow" // should return true since both words "Hello" and "Stackoverflow" are in the array
As suggested in this other SO thread, I tried to use the every() method and the indexOf() method to test if every split string can be found in the array or not as seen in the following Code Snippet but it's not working:
var arr = ["hello", "have", "a", "good", "day", "stackoverflow"];
function checkString(wordArray, str)
{
// split the camelCase words
var x = str.replace(/([A-Z])/g, ' $1').split(" ");
return x.every(e => {
return wordArray.indexOf(e.toLowerCase()) >= 0;
});
}
console.log("should return true ->" + checkString(arr, "HelloStackoverflow"));
console.log("should return false ->" + checkString(arr, "itIsAwfullyColdDayToday"));
What am I doing wrong?