자바스크립트

[JavaScript] 프로그래머스 문자열 뒤집기

코딩하는둥이 2023. 7. 13. 12:08
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 설명

문자열 my_string이 매개변수로 주어집니다. my_string을 거꾸로 뒤집은 문자열을 return하도록 solution 함수를 완성해주세요.

 

입출력 예

my_string result
"jaron" "noraj"
"bread" "daerb"

 

코드 

function solution(my_string) {
    return answer = [...my_string].reverse().join("");
}

 

공부

Spread 연산자(...)

 : 기존 배열이나 객체의 전체 또는 일부를 다른 배열이나 객체로 빠르게 복사할 수 있습니다. 

배열 요소 분해

const numbers = [1, 2, 3];
console.log(...numbers); // 1 2 3

배열 복사

const arr1 = [1, 2];
const arr2 = [3, 4];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4]

배열 합치기

const arr1 = [1, 2];
const arr2 = [3, 4];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4]

 

jion

 : 배열의 모든 요소를 하나의 문자열로 결합하는 역할을 합니다. 

const fruits = ['a', 'b', 'c'];

const joined = fruits.join(', ');
console.log(joined);
// 출력 결과: "a, b, c"