Advertisement
elena1234

Encode and Decode Message ( JavaScript , ASCII) )

Nov 5th, 2021
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function encodeAndDecodeMessages() {
  2.     let encodeButton = document.querySelectorAll("button")[0];
  3.     let decodeButton = document.querySelectorAll("button")[1];
  4.     let textAreaForDecodeMessage = document.querySelectorAll("textarea")[1];
  5.  
  6.     encodeButton.addEventListener("click", function () {
  7.         let textAreaForEncodeMessage = document.querySelectorAll("textarea")[0];
  8.         let message = textAreaForEncodeMessage.value;
  9.         let encodeMessage = encode(message);
  10.         textAreaForEncodeMessage.value = "";
  11.         textAreaForDecodeMessage.value = encodeMessage;
  12.     })
  13.  
  14.     decodeButton.addEventListener("click", function () {
  15.         let encodeMessage = textAreaForDecodeMessage.value;
  16.         let decodeMessage = decode(encodeMessage);
  17.         textAreaForDecodeMessage.value = decodeMessage;
  18.     })
  19.  
  20.  
  21.     function encode(message) {
  22.         let encodedMessage = '';
  23.         for (let i = 0; i < message.length; i++) {
  24.             encodedMessage += String.fromCharCode(ascii(`${message[i]}`) + 1);
  25.         }
  26.         return encodedMessage;
  27.     }
  28.  
  29.     function decode(encodeMessage) {
  30.         let decodeMessage = '';
  31.         for (let i = 0; i < encodeMessage.length; i++) {
  32.             decodeMessage += String.fromCharCode(ascii(`${encodeMessage[i]}`) - 1);
  33.         }
  34.         return decodeMessage;
  35.     }
  36.  
  37.     function ascii(a) {
  38.         return a.charCodeAt(0);
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement