Coding Test/프로그래머스 Lv.0
배열 두 배 만들기 (Map함수, For문, 곱하기 연산자)
sangwoo_rhie
2023. 6. 10. 10:46
Map 함수 사용.
const array1 = [1, 4, 9, 16];
// Pass a function to map
const map1 = array1.map(x => x * 2);
map함수는 전체 배열에 곱하기, 더하기, 나누기 등의 연산을 사용함.
▼
function solution (numbers){
const answer = numbers.map(x => x*2)
return answer;
}
For문 사용
▼
function solution (numbers){
for (let i = 0; i < numbers.length; i++){
numbers[i]*=2
}
return numbers;
}
곱하기 연산자(*)는 두 연산자의 곱을 구합니다.
곱하기 할당 (*=)
곱하기 할당 연산자(*=)는 오른쪽 피연산자의 값을 변수에 곱한 결과를 다시 변수에 할당합니다.'
여기선 곱하기 할당 사용해야함.