I am struggling with a codewars challenge:
Complete the function scramble(str1, str2)
that returns true
if a portion of str1
characters can be rearranged to match str2
, otherwise returns false
.
it fails the tests console.log(scramble(‘scriptjavx’,‘javascript’)) and console.log(scramble(‘javscripts’,‘javascript’)).
I cannot figure out why. All help is appreciated
function scramble(str1, str2) {
let arr1 = str1.split("")
let arr2 = str2.split("")
for (let i = 0; i < arr1.length; i++){
for (let j = 0; j < arr2.length; j++){
if (arr1[i] === arr2[j]){
arr2.splice(j, 1)
if (arr2.length === 0){
return true
}
}}}
return false
}