코플릿/재귀

10_and

테오구 2021. 10. 30. 20:38
728x90

문제

배열을 입력받아 모든 요소의 논리곱(and)을 리턴해야 합니다.

입력

let output = and([true, true, true]);
console.log(output); // --> true

output = and([true, true, false]);
console.log(output); // --> false

 

 

 

function and(arr) {
  if (arr.length === 0) {
    return true;
  }

  // const [head, ...tail] = arr;
  const head = arr[0];
  const tail = arr.slice(1);

  // if (head === false) {
  //   return false;
  // }

  return head && and(tail);
}

 

728x90

'코플릿 > 재귀' 카테고리의 다른 글

11_or  (0) 2021.10.31
12_reverseArr  (0) 2021.10.31
13_findMatryoshka  (0) 2021.10.31
14_unpackGiftbox  (0) 2021.10.31
15_flattenArr  (0) 2021.10.31