Question: Javascript response please. Hello I'm not able to get the submitted code to work. Does anyone have any input or a new solution? Thank you.

Javascript response please. Hello I'm not able to get the submitted code to work. Does anyone have any input or a new solution? Thank you.

Challenge - Have the function CaesarCipher(str,num) take the str parameter and perform a Caesar Cipher shift on it using the num parameter as the shifting number. A Caesar Cipher works by shifting each letter in the string N places in the alphabet (in this case N will be num). Punctuation, spaces, and capitalization should remain intact.

For example if the string is "Caesar Cipher" and num is 2 the output should be "Ecguct Ekrjgt".

Examples Input: "Hello" & num = 4

Output: Lipps

Input: "abc" & num = 0

Output: abc

Function to be solved: function

CaesarCipher(str,num) {

// code goes here

return str; }

// keep this function call here

console.log(CaesarCipher(readline()));

Expert Answer that needs tweaked or a new solution

function CaesarCipher(str, num) {

let result = "";

for (let i = 0; i < str.length; i++) { if (str[i] >= 'a' && str[i] <= 'z') { result += String.fromCharCode('a'.charCodeAt(0) + (str[i].charCodeAt(0) - 'a'.charCodeAt(0) + num) % 26);

} else if (str[i] >= 'A' && str[i] <= 'Z') { result += String.fromCharCode('A'.charCodeAt(0) + (str[i].charCodeAt(0) - 'A'.charCodeAt(0) + num) % 26); } else { result += str[i]; }

}

str = result;

return str;

}

// keep this function call here

console.log(CaesarCipher(readline()));

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!