Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda