Kimyeongkyung
[2023.11.12] 마지막 두 원소 본문
정수 리스트 num_list가 주어질 때, 마지막 원소가 그전 원소보다 크면 마지막 원소에서 그전 원소를 뺀 값을 마지막 원소가 그전 원소보다 크지 않다면 마지막 원소를 두 배한 값을 추가하여 return하도록 solution 함수를 완성해주세요.
제한사항
- 2 ≤ num_list의 길이 ≤ 10
- 1 ≤ num_list의 원소 ≤ 9
답안
const solution = (num_list) => {
let answer = [...num_list];
const lastElement = num_list.at(-1);
const secondToLastElement = num_list.at(-2);
if(lastElement > secondToLastElement){
answer.push(lastElement - secondToLastElement);
}else if(lastElement <= secondToLastElement){
answer.push(lastElement*2)
}
return answer;
}
'[JS] 프로그래머스 코딩테스트 Lv.0' 카테고리의 다른 글
[2023.11.25] 개미군단 (0) | 2023.11.25 |
---|---|
[2023.11.13] A 강조하기 (1) | 2023.11.13 |
[2023.11.11] 접미사인지 확인하기 (0) | 2023.11.11 |
[2023.11.10] 원소들의 곱과 합 & 공배수 & 문자열의 뒤의 n글자 (0) | 2023.11.10 |
[2023.11.9] 수 조작하기1 & 뒤에서 5등 위로 (0) | 2023.11.09 |