📚
Square Array Values
Square Array Values - Explanation
쉬움 배열 O(n) O(n)
Problem Summary
Write a function that squares every number in an array.
Go to Problem →Detailed Explanation
이 문제는 **map 메서드**로 배열의 각 요소를 제곱하는 방법을 학습합니다. **제곱 계산 방법** - `x * x`: 가장 직관적 - `x ** 2`: 거듭제곱 연산자 - `Math.pow(x, 2)`: Math 메서드 **[1, 2, 3] 제곱 과정** - 1 * 1 = 1 - 2 * 2 = 4 - 3 * 3 = 9 - 결과: [1, 4, 9] **음수의 제곱** 음수를 제곱하면 양수가 됩니다: - (-2) * (-2) = 4
Solution Code
solution.js
function squareArray(arr) {
return arr.map(x => x * x);
}Key Concepts from This Problem
1. map 메서드
2. 제곱 연산
3. 배열 변환
4. 거듭제곱
Common Mistakes
✗ x * 2는 두 배이고, x * x가 제곱입니다
✗ ** 연산자로 x ** 2도 가능합니다
✗ 음수의 제곱은 양수가 됩니다
Hints
Hint 1: map으로 각 요소를 제곱하세요.
Complexity Analysis
Time Complexity
O(n)
Grows linearly with input size
Space Complexity
O(n)
Uses memory proportional to input size
Related Tags
#배열 #map #수학