IT/Programmers
[Programmers] 둘만의 암호
개발자희망생고롸파덕
2024. 2. 29. 14:46
사용언어 : javascript
lv.1
문제 풀이 소요 시간 : 26분 15초
#문제
#제출코드
function solution(s, skip, index) {
let answer = '';
const skipBook = skip.split('').map((item) => item.charCodeAt());
s.split('').map((item) => {
let asc = item.charCodeAt();
for (let i = 0; i < index; i++) {
asc +=1;
if (asc > 122) {
asc -= 26;
}
while(skipBook.includes(asc)){
asc += 1;
if (asc > 122) {
asc -= 26;
}
}
}
answer += String.fromCharCode(asc);
});
return answer;
}
아스키 코드를 이용한 문제 풀이..!
#다른 풀이
function solution(s, skip, index) {
const alphabet = ["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"].filter(c => !skip.includes(c));
return s.split("").map(c => alphabet[(alphabet.indexOf(c) + index) % alphabet.length]).join("");
}