군 전략가 머쓱이는 전쟁 중 적군이 다음과 같은 암호 체계를 사용한다는 것을 알아냈습니다.
- 암호화된 문자열 cipher를 주고받습니다.
- 그 문자열에서 code의 배수 번째 글자만 진짜 암호입니다.
문자열 cipher와 정수 code가 매개변수로 주어질 때 해독된 암호 문자열을 return하도록 solution 함수를 완성해주세요.
1
≤ cipher
의 길이 ≤ 1,000
1
≤ code
≤ cipher
의 길이cipher
는 소문자와 공백으로만 구성되어 있습니다.배열의 인덱스 값 확인
변수명 arr
인 배열이 있을 경우, arr
에서 인덱스가 i
인 값을 확인하기 위해 at
사용
const arr = [1, 2, "three", 4, 5, true, false];
console.log(arr.at(0)); // 1
console.log(arr.at(2)); // "three"
console.log(arr.at(-1)); // false
console.log(arr.at(-3)); // 5
function solution(cipher, code) {
var answer = '';
for (i=0; i<cipher.length; i++){
if (i % code == code-1){
answer += cipher.at(i)
}
}
return answer;
}