Tell us what’s happening:
the result is vertically how can I fix it?
Describe tu problema en detalle aquí.
Your code so far
function rot13(str) {
const alphaNum = ['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','?','!','.',' '];
str = str.split("");
for (let i = 0 ;i < str.length;i ++){
let indexRes = [alphaNum.indexOf(str[i])];
let resInd = indexRes.map((num) => {
if (num >= 13 & num < 26){
return num - 13;
}
if (num <= 13 && num >= 1){
return num + 13;
}
if (num >= 26) {
return num;
}
});
const resultAlph = [];
for (let k = 0;k < resInd.length;k++){
for(let h = 0;h < alphaNum.length;h++){
if (resInd[k] === h){
resultAlph.push(alphaNum[h]);}
}
const resultRot = resultAlph.join();
console.log(resultRot);
}
}
}
rot13("SERR PBQR PNZC");
Your browser information:
El agente de usuario es: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Challenge Information:
Algoritmos de JavaScript y proyectos de estructuras de datos - Cifrado César
The problem is that you are only printing each letter as a new value
something like this,
in each iteration, “resultRot” has a new value
but you should concatenate the letters to make something like this
as you can see, in “resultRot” a new letter is added to make a bigger string
also, you aren’t returning anything. You should return something in your function.
No puedo , me dice read only
Comparte tu código modificado para correrlo.
Pero recuerda que no puedes hacer esto:
const resultRot = 1
resultRot =2
porque “const” es una constante, lo que significa que no puedes modificar su valor, tendrías que usar algo diferente a “const”
La respuesta sigue saliendo vertical
function rot13(str) {
const alphaNum = [‘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’,‘?’,‘!’,‘.’,’ '];
str = str.split(“”);
for (let i = 0 ;i < str.length;i ++){
let indexRes = [alphaNum.indexOf(str[i])];
let resInd = indexRes.map((num) => {
if (num >= 13 & num < 26){
return num - 13;
}
if (num <= 13 && num >= 1){
return num + 13;
}
if (num >= 26) {
return num;
}
});
let resultAlph = “”;
for (let k = 0;k < resInd.length;k++){
for(let h = 0;h < alphaNum.length;h++){
if (resInd[k] === h){
resultAlph += alphaNum[h];}
}
console.log(resultAlph);
}
}
return str;
}
rot13(“SERR PBQR PNZC”);
Ahora estás retornando el valor de str, pero str es la cadena original solo la separase en un array
Ibas mas o menos bien con el código que tenías, lo que tenías que modificar un poco es “resultRot”,
también es “resultRot” lo que tienes que retornar.
Aunque tienes un problema con tu código la letra A no la convierte a N.
function rot13(str) {
const alphaNum = ['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', '?', '!', '.', ' '];
str = str.split("");
let resultRot = ""
for (let i = 0; i < str.length; i++) {
let indexRes = [alphaNum.indexOf(str[i])];
let resInd = indexRes.map((num) => {
if (num >= 13 & num < 26) {
return num - 13;
}
if (num <= 13 && num >= 1) {
return num + 13;
}
if (num >= 26) {
return num;
}
});
const resultAlph = [];
for (let k = 0; k < resInd.length; k++) {
for (let h = 0; h < alphaNum.length; h++) {
if (resInd[k] === h) {
resultAlph.push(alphaNum[h]);
}
}
resultRot += resultAlph.join();
console.log(resultRot);
}
}
}
rot13("SERR PBQR PNZC");
Cuando añadas codigo usa el boton </>

y pega tu codigo entre las comillas triples
