loading...

re: Daily Challenge #42 - Caesar Cipher VIEW POST

FULL DISCUSSION
 

JavaScript

//-----------------------------------------------------------------------
//-----------------------------Caesar Cipher-----------------------------
//-----------------------------------------------------------------------
//a == 97 z == 122
//A == 65 Z == 90

const cypher = function(msg,key)
{
    let inRange = (number, x, y) =>
    {
        if(number >= x && number <= y)
        {
            return true;
        }
        return false;
    }
    let newMsg = "";
    for(let i = 0; i < msg.length; i++)
    {
        let charUnicode = msg.codePointAt(i);
        if(inRange(charUnicode, 97, 122) || inRange(charUnicode, 65, 90))
        {
            newMsg += String.fromCodePoint(charUnicode + key);
        }else
        {
            newMsg += msg[i];
        }
    }
    return newMsg;
}

let message = "This took hella lotta time !";
console.log(cypher(message, 4));
code of conduct - report abuse