[프로그래머스] 코딩테스트 자연수 뒤집어 배열로 만들기
자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다.
n | result |
---|---|
12345 | [5,4,3,2,1] |
function solution(n) {
const answer = [];
[...n.toString()].forEach(s => answer.push(Number(s)));
return answer.reverse();
}
function solution(n) {
return n.toString().split('').reverse().map(o => o = parseInt(o));
}
🤍 map()
const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1); // [2, 8, 18, 32]