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

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

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 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!