[js][hackerRank] Day7 : arrays

florentyoon·2021년 2월 22일
0

코딩테스트

목록 보기
4/6

문제 :

Day 7: Arrays
Objective
Today, we’re learning about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given an array, A, of N integers, print A‘s elements in reverse order as a single line of space-separated numbers.

Input Format
The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers describing array A‘s elements.

Constraints
1 <= N <= 1000
1 <= Ai <= 10000, where Ai is the ith integer in the array.
Output Format
Print the elements of array A in reverse order as a single line of space-separated numbers.

Sample Input
1
2
4
1 4 3 2
##Sample Output

1
2 3 4 1

문제 풀이 :

주어진 배열을 거꾸로 출력하기. 다만 값의 오름차순이나 내림차순이 아닌 맨 뒤 숫자부터 차례대로 출력.

코드 :

function main() {
    const n = parseInt(readLine(), 10);

    const arr = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10));
    
    let answer = "";  // 문자열로 저장할 변수 생성
    
    for(var i=n; i > 0;  i--){
        answer += arr[i-1] + " ";  // 배열 크기로 주어지는 n을 기본값으로 초기화함. 
         							  배열의 크기만큼 for문 돌리면서 answer에 추가
       
    }
     console.log(answer);  // 결과 출력
}
profile
florentyoon의 IT 세상

0개의 댓글