๐
Array Average
Array Average์ฌ์ ๋ฐฐ์ด +10pts
Problem
Write a function that returns the average of all numbers in an array.
Examples
Input:
average([1, 2, 3, 4, 5])Output:
3Explanation
์ด ๋ฌธ์ ๋ **ํฉ๊ณ ๊ณ์ฐ**๊ณผ **ํ๊ท ๊ณต์**์ ๊ฒฐํฉํ์ฌ ๋ฐฐ์ด์ ํ๊ท ์ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ ํ์ตํฉ๋๋ค. **ํ๊ท ๊ณต์** ํ๊ท = ์ ์ฒด ํฉ๊ณ / ์์ ๊ฐ์ **reduce๋ก ํฉ๊ณ ๊ตฌํ๊ธฐ** `arr.reduce((sum, n) => sum + n, 0)` - ์ด๊ธฐ๊ฐ 0๋ถํฐ ์์ - ๊ฐ ์์๋ฅผ ๋์ ํ์ฌ ๋ํจ **๋น ๋ฐฐ์ด ์ฒ๋ฆฌ** ๋น ๋ฐฐ์ด์ ๊ฒฝ์ฐ 0์ผ๋ก ๋๋๊ธฐ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก ๋จผ์ ์ฒดํฌํฉ๋๋ค: ```javascript if (arr.length === 0) return 0; ``` **[1, 2, 3, 4, 5]์ ํ๊ท ๊ณ์ฐ*...
View detailed explanation โKey Concepts
ํ๊ท ๊ณ์ฐ ๊ณต์ reduce๋ก ํฉ๊ณ ๋น ๋ฐฐ์ด ์์ธ์ฒ๋ฆฌ ๋๋์
์ฐ์ฐ
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.