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
match2("adipisci","sciipx") returns false because x does not exist in variable
match3["adipisci","adipisci"] returns true in array element 1 and 2 if "sciip" is searched
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
                // increment number of times the search term in found
                while(pages[ii].substr(i,1)!=" ") {
                    // move to the next word but checking for spaces
        // update the number of times the search term is found in that page
        // 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!

6 Years
Discussion Span
Last Post by AleMonteiro

Jean, I didn't fully understood what you are trying to do, but I suggest you use Regular Expression for such comparisons.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.