Session 6
Exercise
Create a function that accepts a string, and which encrypts that string using a Caesar cipher, returning the ciphertext to the caller. Create a second function that does the reverse of the first, in that it accepts a ciphertext and converts it back in to its corresponding plaintext. That is, using an offset of three, the first function would convert ‘ABC’ into ‘DEF’ while the second would convert ‘DEF’ back into ‘ABC’..
function createCypher(inp) { let counter = 0; let length = inp.length; let idx = 0; let codeToChar = 0; let outStr = ''; while (counter < length) { idx = inp[counter]; idx = idx.charCodeAt() + 3; codeToChar = String.fromCharCode(idx); outStr = outStr + codeToChar; counter++; } console.log(outStr); return inp;}createCypher('david');//returns 'gdylg'function decodeCypher(inp) { let counter = 0; let length = inp.length; let idx = 0; let codeToChar = 0; let outStr = ''; while (counter < length) { idx = inp[counter]; idx = idx.charCodeAt() - 3; codeToChar = String.fromCharCode(idx); outStr = outStr + codeToChar; counter++; } console.log(outStr); return inp;}decodeCypher('gdylg');// returns 'david'