๐
Array Product
Array Product์ฌ์ ๋ฐฐ์ด +10pts
Problem
Write a function that returns the product of all numbers in an array.
Examples
Input:
product([1, 2, 3, 4])Output:
24Explanation
์ด ๋ฌธ์ ๋ **reduce ๋ฉ์๋**๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ๊ณฑํ๋ ๋ฐฉ๋ฒ์ ํ์ตํฉ๋๋ค. ## ํต์ฌ ๊ฐ๋ : reduce๋ก ๊ณฑ์ ๋์ `reduce`๋ ๋ฐฐ์ด์ ํ๋์ ๊ฐ์ผ๋ก ์ถ์ํ ๋ ์ฌ์ฉํฉ๋๋ค. ํฉ๊ณ๋ฟ ์๋๋ผ ๊ณฑ์ ์๋ ํ์ฉํ ์ ์์ต๋๋ค. ### ์ ๊ทผ ๋ฐฉ๋ฒ 1. ๋น ๋ฐฐ์ด ์ฒดํฌ - ๋น ๋ฐฐ์ด์ด๋ฉด 0 ๋ฐํ 2. reduce๋ก ๋ชจ๋ ์์ ๊ณฑํ๊ธฐ - ์ด๊ธฐ๊ฐ 1 ์ฌ์ฉ ### ์ด๊ธฐ๊ฐ์ด ์ค์ํ ์ด์ - ํฉ๊ณ: ์ด๊ธฐ๊ฐ 0 (0 + a + b + c...) - ๊ณฑ์ : ์ด๊ธฐ๊ฐ 1 (1 * a * b * c...) - ๊ณฑ์ ์์ 0์ ์ด๊ธฐ๊ฐ์ผ...
View detailed explanation โKey Concepts
reduce ๋ฉ์๋ ๊ณฑ์
๋์ ์ด๊ธฐ๊ฐ์ ์ค์์ฑ ๋น ๋ฐฐ์ด ์ฒ๋ฆฌ
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.