선물 상자에 대한 정보를 담은 배열과 문자열을 입력받아 조건에 맞는 선물이 있는지 여부를 리턴해야 한다.
주의사항
boolean 타입을 리턴unpackGiftbox는 재귀함수의 형태로 작성for, while) 사용이 가능입출력 예시
const giftBox = ['macbook', 'mugcup', ['eyephone', 'postcard'], 'money'];
let output = unpackGiftbox(giftBox, 'iphone');
console.log(output); // --> false
풀이방법
function unpackGiftbox(giftBox, wish) {
if (giftBox.length === 0 || wish === '') {
return false;
}
let binarr = [];
for (let i = 0; i < giftBox.length; i++) {
if (giftBox[i] === wish) {
return true;
} else if (Array.isArray(giftBox[i])) {
binarr = binarr.concat(giftBox[i]);
}
}
if (binarr.length > 0) {
return unpackGiftbox(binarr, wish);
}
return false;
}