unction solution(s, n) {
let arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
let arr2 = arr.map((i)=> i.toLowerCase() )
let s1 = [...s]
let s2 = []
for(i=0; i<s1.length; i++){
let p1 = arr.indexOf(s1[i])
let p2 = arr2.indexOf(s1[i])
if(p1 === -1 && p2 === -1){
s2.push(' ')}
else if(p2=== -1 && p1 !== -1 ){
if (p1+n > 25){
s2.push(arr[p1+n-26])
}else {
s2.push(arr[p1+n]) }
} else if (p1=== -1 && p2 !== -1){
if (p2+n > 25){
s2.push(arr2[p2+n-26])
}else {
s2.push(arr2[p2+n])}
}}
return s2.join("")
}
function solution(s, n) {
let arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
let arr2 = arr.map((i)=> i.toLowerCase() )
let s1 = [...s]
let s2 = []
for(i=0; i<s1.length; i++){
let p1 = arr.indexOf(s1[i])
let p2 = arr2.indexOf(s1[i])
if(p1 === -1 && p2 === -1){
s2.push(' ')}
else if(p2=== -1 && p1 !== -1 ){
if (p1+n > 25){
s2.push(arr[(p1+n)%26]) // 고친부분
}else {
s2.push(arr[p1+n]) }
} else if (p1=== -1 && p2 !== -1){
if (p2+n > 25){
s2.push(arr2[(p2+n)%26]) // 고친부분
}else {
s2.push(arr2[p2+n])}
}}
return s2.join("")
}
조건을 잘 파악해서 똑똑하게 품
(조건 n이 25이하)
출처
https://velog.io/@zi12i/프로그래머스-코딩테스트-연습-시저-암호-javascript
function solution(s, n) {
let text = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
let answer = ""
for(let i = 0; i < s.length; i++){
if(s[i] === " "){
answer = answer + " "
} else {
let newIndex = text.indexOf(s[i]) + n
answer = answer + text.charAt(newIndex)
}
}return answer
}
.charAt(i)
문자열도 인덱스 0부터 시작하고 i번째에 있는 문자를 반환한다.
toLowerCase()!