Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions