Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
¿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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!