I am having trouble with below question. I basically have to write a code/function that returns true if a portion of str1 can be rearraged to str2.
Write function scramble(str1,str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
For example: str1 is 'rkqodlw' and str2 is 'world' the output should return true. str1 is 'cedewaraaossoqqyt' and str2 is 'codewars' should return true. str1 is 'katas' and str2 is 'steak' should return false.
Only lower case letters will be used (a-z). No punctuation or digits will be included. Performance needs to be considered.
Below is the current code I have:
function scramble(str1, str2) {
var first; //longer string
var second; //shorter string
if(str1 || str2 === "undefined") {
return false;
}
if(str1.length > str2.length) {
first = str1;
second = str2
} else if(str2.length > str1.length) {
first = str2;
second = str1;
}
for (i=0; i<second.length; i++) {
if (first.indexOf(second[i]) === -1) {
return false;
}
}
return true;
}
Could you please help me with this question?
str2
are instr1
, and if so, return true ?if(str1 || str2 === "undefined") {
isn't doing what you probably think its doing