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

224
Vistas
How to retain spaces in input strings of Caesar Cipher for javascript?

This project is in javascript. I need to make sure the output retains spaces found in the string that is inputted into the function. The test I am trying to pass is calling the function for the term "hello world" with a shift of 13 letters. From this code, the result is "uryybjbeyq" and "uryyb jbeyq" is expected. I have identified I need an if statement which I have included already, but not sure what command I should include before the continue keyword that will insert the space needed. I am a beginner and this is only my 3rd project so any assistance would be appreciated. Please find the corresponding code below.

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

You can add the white space like this:

encryptStr += " ";

I tried your code and I ran into an error... All letters were the same. Here is how I did it:

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