Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
¿Cómo retener espacios en las cadenas de entrada de Caesar Cipher para javascript?

Este proyecto está en javascript. Necesito asegurarme de que la salida conserve los espacios que se encuentran en la cadena que se ingresa en la función. La prueba que estoy tratando de pasar es llamar a la función para el término "hola mundo" con un desplazamiento de 13 letras. A partir de este código, el resultado es "uryybjbeyq" y se espera "uryyb jbeyq". He identificado que necesito una declaración if que ya he incluido, pero no estoy seguro de qué comando debo incluir antes de la palabra clave continuar que insertará el espacio necesario. Soy un principiante y este es solo mi tercer proyecto, por lo que agradecería cualquier ayuda. Encuentre el código correspondiente a continuación.

 function caesarCypher(string, num){ // line below is the encrypted string we will return from the function const letters = 'abcdefghijklmnopqrstuvwxyz'; let encryptStr = "" // loop through every character in the inputted string for(let i = 0; i < string.length; i++){ // line below will look up index of char in alphabet let char = string[i]; /* if statement below is attempting to identify the space in the original string and then add a command that will add a space to the encrypted string then continue to work through the rest of the input */ if (char === " ") { //need something to put on this line to insert space; continue; } let index = letters.indexOf(char); // index + num below will give us the letter 7 spots over let newIndex = index + num; // if statement below makes the function loop back around if (newIndex > 26) { newIndex = newIndex - 26; } let newChar = letters[newIndex]; encryptStr += newChar; } return encryptStr; } /* invoke the function with these parameters to pass the test-- expected result is 'uryyb jbeyq'*/ caesarCypher("hello world", 13)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Puede agregar el espacio en blanco de esta manera:

 encryptStr += " ";

Probé tu código y me encontré con un error... Todas las letras eran iguales. Así es como lo hice:

 function caesarCypher(string, num){ let encryptStr = ""; for(let i = 0; i < string.length; i++){ let char = string[i]; if (char === " ") { encryptStr += " "; //This adds a white space. continue; } // If you want to keep the case of a letter, skip the // "toLowerCase()" and extend the condition below. let asciiCode = string.toLowerCase().charCodeAt(i) + num; if(asciiCode > 122) { asciiCode -= 26; } encryptStr += String.fromCharCode(asciiCode); } return encryptStr; }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda