Skip to content

Instantly share code, notes, and snippets.

@rvbsanjose
Created April 17, 2014 22:32
Show Gist options
  • Save rvbsanjose/11015212 to your computer and use it in GitHub Desktop.
Save rvbsanjose/11015212 to your computer and use it in GitHub Desktop.
// Using the JavaScript language, have the function StringScramble(str1,str2) take both parameters being passed and return the string true if a portion of str1 characters can be rearranged to match str2, otherwise return the string false. For example: if str1 is "rkqodlw" and str2 is "world" the output should return true. Punctuation and symbols will not be entered with the parameters.
// Input = "cdore" & str2= "coder" Output = "true"
// Input = "h3llko" & str2 = "hello" Output = "false"
function StringScramble( str1, str2 ) {
var output = '';
for ( var i = 0; i < str2.length; i++ ) {
var pattern = new RegExp( str2[i] );
if ( str1.match( pattern ) && str1.match( pattern )[0] ) {
output += str1.match( pattern )[0];
str1 = str1.replace( str1.match( pattern )[0], '' );
}
}
return output == str2 ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment