Created
April 17, 2014 22:32
-
-
Save rvbsanjose/11015212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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