Solución
solution.tsTypeScript
function caesarCipher(text: string, shift: number): string {
const abc = 'abcdefghijklmnopqrstuvwxyz';
const mayusAbc = abc.toUpperCase();
let resp = ''
text.split('').forEach(char => {
if (abc.includes(char) || mayusAbc.includes(char)) {
const isUpper = /[A-Z]/.test(char)
if (isUpper) {
const index = mayusAbc.indexOf(char)
const newIndex = index + shift
if (newIndex > abc.length - 1) {
resp += mayusAbc[newIndex - abc.length]
} else {
resp += mayusAbc[newIndex]
}
} else {
const index = abc.indexOf(char)
const newIndex = index + shift
if (newIndex > abc.length - 1) {
resp += abc[newIndex - abc.length]
} else {
resp += abc[newIndex]
}
}
} else {
resp += char
}
})
return resp;
}
// No modificar: necesario para evaluar el resultado.
export { caesarCipher };0respuestas