I'm writing a piece of JS code that returns a result of true if the pattern appears in string as a substring (case sensitive) but would like to extend its functionality to returns true if all the individual characters of pattern appear in string (regardless of order).
For example:
This is what the program currently does:
match1("adipisci","pis") returns true
Whereas I would now like it to do this:
match1("adipisci","sciip") returns true
2
match2("adipisci","sciipx") returns false because x does not exist in variable
3
match3["adipisci","adipisci"] returns true in array element 1 and 2 if "sciip" is searched
4
match4["adipisci","adipiscix"] returns false in array element 1 and true in array element 2 if "sciipx" is searched
I am having difficulty implementing this into my code... this is what I've done so far:
var pages=[
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Nulla imperdiet laoreet neque.",
"Praesent at gravida nisl. Quisque tincidunt, est ac porta malesuada, augue lorem posuere lacus, vitae commodo purus nunc et lacus."
];
var search_term = prompt('Type in search term: ','Search word');
// ask which search term the user wants to search for with default being 'Search Term'
function find_s(w) {
var indexes = [0,0,0] // create an array to hold pages where search term found
var iii = 0 // create variable to increment the number of finds in each page
for (var ii=0;ii<pages.length;ii++) {
// for each page in the array
for (var i=0;i<pages[ii].length;i++) {
// for each character in the chosen page
if (pages[ii].substr(i,w.length).toLowerCase()==w.substr(0,w.length).toLowerCase()) {
// check to see if the search term is there ignoring case
iii++;
// increment number of times the search term in found
while(pages[ii].substr(i,1)!=" ") {
// move to the next word but checking for spaces
i++;
}
}
}
indexes[ii]=iii;
// update the number of times the search term is found in that page
iii=0;
// reset counter for next page search
}
return (w + " was found in this may times in each page " + indexes);
// let the user know the result
}
alert (find_s(search_term));
Thanks for the help in advance!