Crystal
Crystal

Reputation: 1505

How can I find if ALL of string2 letters are also contained within string1 somewhere?

I am trying to compare two strings to see if ALL of one of the string's input is also within another string, regardless of order.

So far I have the following code...

What am I doing wrong?

var str1= "rkqodlw"
var str2= "world"

StringScrambler(str1, str2);

function StringScrambler(str1, str2) {
    var string1= str1.split("").sort();
        console.log(string1);
    var string2 = str2.split("").sort();
        console.log(string2);
    matches = [];
    for (i=0; i< string1.length; i++) {
        for (j=0; j<string2.length; i++) {
            while (j === i) {
              matches.push(j);
              console.log(matches);
              var matchSort = matches.sort();
              console.log(matchSort);
                if (matchSort === string2) {
                    return true;
                }else {
                    return false;
                }
            }
        }
    }
}

Upvotes: 2

Views: 133

Answers (7)

Taff
Taff

Reputation: 231

var str1= "rkqodlw";
var str2= "world";

function test($str1, $str2) {
  var string2 = str2.split("");
  for(var i=0; i<string2.length; i++) {
    if (str1.indexOf(string2[i]) == -1) {
      return false;
    }
  }
  return true;
}

Upvotes: 1

walmik
walmik

Reputation: 1452

All the answers this far work fine but they will not work for words with double letters in the second string but not in the first (for eg. 'worlld' - notice the double L). The trick is to affect the first word such that it removes the found character(s) so that the same letter is not checked again. Something like this would do the trick:

// Check if the second string's characters are
// found in the first string
function StringScrambler(str1, str2) {
  var arr1 = str1.split(''),
      arr2 = str2.split(''),
      isATrueSubset = true,
      indexOfChar;

  arr2.forEach(function(char) {
    indexOfChar = arr1.indexOf(char);
    if (indexOfChar > -1) {
      // Remove the character that was found
      // to avoid matching against it again
      arr1.splice(indexOfChar, 1);
    } else {
      isATrueSubset = false;
      // No need to continue
      return;
    }
  });

  console.log(isATrueSubset);
  return isATrueSubset;
}

StringScrambler('rkqodlw ', 'world '); // outputs true
StringScrambler('rkqodlw ', 'worlld '); // outputs false

Upvotes: 1

maioman
maioman

Reputation: 18734

try this:

var str1= "rkqodlw"
var str2= "world"

StringScrambler(str1, str2);

  function StringScrambler(str1, str2) {
  var string1 = str1.split("").sort();

  var string2 = str2.split("").sort();

  matches = [];
   for (i = 0; i < string1.length; i++) {
      if (string2.indexOf(string1[i]) > -1) matches.push(string1[i]);
   }
return matches
}

Upvotes: 0

robin
robin

Reputation: 1925

This will compare each words of the second string in the first one and if its present it will be added in the mathes array.

var str1= "rkqodlw";
var str2= "world2";

StringScrambler(str1, str2);

function StringScrambler(str1, str2) {

var string2 = str2.split("").sort();

console.log(string2);
matches = [];
for (j=0; j<string2.length; j++) {
    if(str1.indexOf(string2[j]) > -1){
         matches.push(string2[j]);
        console.log(string2[j]);    
    }
}
console.log(matches);

}

Upvotes: 0

mplungjan
mplungjan

Reputation: 177691

If str2 is always a subset of str1, then this answer can be used

Compute intersection of two arrays in JavaScript

var arr1 = "rkqodlw".split("");
var arr2 = "world".split("");

var commonValues = arr2.filter(function(value) { 
  return arr1.indexOf(value) > -1; 
});

alert(commonValues.join(""))

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881243

You can use the following code to do this task:

alert (AllFirstInSecond("world", "rkqodlw"));
alert (AllFirstInSecond("worldz", "rkqodlw"));

function AllFirstInSecond(str1, str2) {
    var pos = str1.length - 1;
    while (pos >= 0) {
        if (str2.indexOf(str1.substr(pos--,1)) == -1) {
            return false;
        }
    }
    return true;
}

It simply checks every single character in the first string to see if it's in the second. If not, it returns false.

Only once all have been found does it return true.

There are possibilities for optimisation (every character is checked even if it's a duplicate that's already been checked) but, unless your strings are particularly large, there's probably not much absolute gain to be had.

Upvotes: 0

Lord Midi
Lord Midi

Reputation: 804

var one = "dlrow";
var two = "world";
var allCharsFound = true;
one.split("").map(function(char) {
    if (two.indexOf(char) < 0) {
        allCharsFound = false;
    }
});
console.log(allCharsFound);

Upvotes: 1

Related Questions