|
| 1 | +function solution(numbers, direction) { |
| 2 | + var answer = [...numbers]; |
| 3 | + |
| 4 | + if (direction === "right") { |
| 5 | + const rightNumbers = answer.pop(); |
| 6 | + answer.unshift(rightNumbers); |
| 7 | + } else if (direction === "left") { |
| 8 | + const leftNumbers = answer.shift(); |
| 9 | + answer.push(leftNumbers); |
| 10 | + } |
| 11 | + |
| 12 | + return answer; |
| 13 | +} |
| 14 | + |
| 15 | +/* slice 연습하기 */ |
| 16 | +function solution(numbers, direction) { |
| 17 | + if (direction === "right") { |
| 18 | + const last = numbers.slice(-1); |
| 19 | + const rest = numbers.slice(0, -1); |
| 20 | + return [...last, ...rest]; |
| 21 | + } else if (direction === "left") { |
| 22 | + const first = numbers.slice(0, 1); |
| 23 | + const rest = numbers.slice(1); |
| 24 | + return [...rest, ...first]; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/* splice 연습 */ |
| 29 | +function solution(numbers, direction) { |
| 30 | + if (direction === "right") { |
| 31 | + const last = numbers.splice(-1, 1); |
| 32 | + numbers.unshift(...last); |
| 33 | + } else if (direction === "left") { |
| 34 | + const first = numbers.splice(0, 1); |
| 35 | + numbers.push(...first); |
| 36 | + } |
| 37 | + return numbers; |
| 38 | +} |
0 commit comments