๐Ÿ“š

First Element

First Element
์‰ฌ์›€ ๋ฐฐ์—ด +10pts

Problem

Write a function that returns the first element of an array.

Examples

Input: first([1, 2, 3])
Output: 1

Explanation

์ด ๋ฌธ์ œ๋Š” **๋ฐฐ์—ด ์ธ๋ฑ์‹ฑ**์˜ ๊ธฐ์ดˆ๋ฅผ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค. **๋ฐฐ์—ด ์ธ๋ฑ์Šค** ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค: - arr[0]: ์ฒซ ๋ฒˆ์งธ ์š”์†Œ - arr[1]: ๋‘ ๋ฒˆ์งธ ์š”์†Œ - arr[arr.length - 1]: ๋งˆ์ง€๋ง‰ ์š”์†Œ **์ฒซ ๋ฒˆ์งธ ์š”์†Œ ์ ‘๊ทผ** `arr[0]`์€ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. **๋นˆ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ** ๋นˆ ๋ฐฐ์—ด์—์„œ arr[0]์€ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. **at() ๋ฉ”์„œ๋“œ (ES2022)** `arr.at(0)`๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. at...

View detailed explanation โ†’

Key Concepts

๋ฐฐ์—ด ์ธ๋ฑ์‹ฑ 0-based ์ธ๋ฑ์Šค ์š”์†Œ ์ ‘๊ทผ undefined ๋ฐ˜ํ™˜
Time: O(1) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.