<입문 100문제> Day 10 - 조건문, 배열, 수학, 시뮬레이션
문제
풀이
function solution(numbers, direction) {
let answer = []
if (direction === 'right') {
numbers.unshift(numbers.pop())
} else if (direction === 'left') {
numbers.push(numbers.shift())
}
answer = numbers
return answer
}
해답
function solution(numbers, direction) {
return direction === 'right'
? [numbers[numbers.length - 1], ...numbers.slice(0, numbers.length - 1)]
: [...numbers.slice(1), numbers[0]];
}
function solution(numbers, direction) {
let answer = [];
if ("right" == direction) {
numbers.unshift(numbers.pop());
} else {
numbers.push(numbers.shift());
}
answer = numbers;
return answer;
}
참고자료