길이가 n이고, "수박수박수박수...."와 같은 패턴을 유지하는 문자열을 리턴하는 함수, solution을 완성하세요. 예를들어 n이 4이면 "수박수박"을 리턴하고 3이라면 "수박수"를 리턴하면 됩니다.
function solution(n) {
var answer = Array.from({ length: n }).map((_, index) => (index + 1) % 2 !== 0 ? "수" : "박").join("");
return answer;
}
어떤 문장의 각 알파벳을 일정한 거리만큼 밀어서 다른 알파벳으로 바꾸는 암호화 방식을 시저 암호라고 합니다. 예를 들어 "AB"는 1만큼 밀면 "BC"가 되고, 3만큼 밀면 "DE"가 됩니다. "z"는 1만큼 밀면 "a"가 됩니다. 문자열 s와 거리 n을 입력받아 s를 n만큼 민 암호문을 만드는 함수, solution을 완성해 보세요.
function solution(s, n) {
var answer = '';
for (let i = 0; i < s.length; i++) {
const lowerRegex = /[a-z]/;
const upperRegex = /[A-Z]/;
const charCode = s[i].charCodeAt();
let nextCharCode = charCode + n;
if (new RegExp(lowerRegex).test(s[i])) {
nextCharCode = nextCharCode > 122 ? 97 + (nextCharCode - 123) : nextCharCode;
} else if (new RegExp(upperRegex).test(s[i])) {
nextCharCode = nextCharCode > 90 ? 65 + (nextCharCode - 91) : nextCharCode;
} else {
nextCharCode = charCode;
}
answer += String.fromCharCode(nextCharCode);
}
return answer;
}