Ceaser Cipher JS Playground

This is a JS playground to generate Ceaser Cipher encryption

by Rajdeep Chandra

HTML

<form onsubmit="return ceaserShift(event)" name="myForm" method="post">
  <h4>
   An Example to encrypt your input via Ceaser Cipher
  </h4>
  <label>Input</label>
  <input type="text" placeholder="Enter your text" name="inputTxt" />
  <br>
  <label>OutPut</label>
  <input type="text" placeholder="Enter your text" readonly name="outputTxt" />
  <br>
  <input type="submit" value="Encrypt" />
</form>
<br>

JavaScript

function ceaserShift(e) {
		e.preventDefault();
   let str = document.forms["myForm"]["inputTxt"].value; 
   let amount = 90;
	
  if (amount < 0) {
    return ceaserShift(str, amount + 26)
  }
	
  // we will store the output here
  let output = '';

  for (let i = 0; i < str.length; i++) {
  // holding the value
    let ch = str[i];
		
    if (ch.match(/[a-z]/i)) {
    
    // getting the code for that letter
      let code = str.charCodeAt(i)

      // checking for uppercase letters
      if ((code >= 65) && (code <= 90)) {
        ch = String.fromCharCode(((code - 65 + amount) % 26) + 65);
      }

      // checking for lowercase letters
      else if ((code >= 97) && (code <= 122)) {
        ch = String.fromCharCode(((code - 97 + amount) % 26) + 97);
      }
      
    }
    
    // appending the output
    
    output += ch;
    
  }
  document.forms["myForm"]["outputTxt"].value = output
  return output;
}