문제
str1과 str2라는 두 개의 문자열을 받는 JavaScript 함수를 작성해야 합니다. str1 문자의 일부가 str2와 일치하도록 재배열될 수 있으면 함수는 true를 반환해야 하고, 그렇지 않으면 false를 반환해야 합니다.
예
다음은 코드입니다 -
const str1 = 'rkqodlw';
const str2 = 'world';
const canForm = (str1 = '', str2 = '') => {
if(str1.length < str2.length){
return false;
};
const res = str2.split('');
str1.split("").forEach(val => {
if(res.includes(val)){
res.splice(res.indexOf(val), 1);
};
});
return res.length === 0;
};
console.log(canForm(str1, str2)); 출력
다음은 콘솔 출력입니다 -
true