λ°°μ΄μ μ λ ₯λ°μ λͺ¨λ μμμ κ³±μ 리ν΄ν΄μΌ ν©λλ€.
let output = arrProduct([1, -2, 1, 3]);
console.log(output); // --> -6
function arrProduct(arr) {
// TODO: μ¬κΈ°μ μ½λλ₯Ό μμ±ν©λλ€.
if(arr.length === 0){
return 1;
}else{
return arr.shift()*arrProduct(arr);
}
}
// shift() λ©μλλ λ°°μ΄μμ 첫 λ²μ§Έ μμλ₯Ό "μ κ±°"νκ³ , "μ κ±°λ μμλ₯Ό λ°ν"ν©λλ€.
// μ΄ λ©μλλ λ°°μ΄μ κΈΈμ΄λ₯Ό λ³νκ² ν©λλ€.(mutable)
function arrProduct(arr) {
if (arr.length === 0) {
return 1;
}
// const [head, ...tail] = arr;
const head = arr[0];
const tail = arr.slice(1);
return head * arrProduct(tail);
}
// λ°λ‘ μ§μ κ²μλ¬ΌμΈ 05_arrSumκ³Ό νμ΄κ° κ°λ€.
λλ shiftλ©μλλ‘ λ νΌλ°μ€ νμ΄μ arr[0]μ κ°μ΄ ννν΄ μ£Όμλ€.