Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. function encodeAndDecodeMessages() {
  2.  
  3. function ascii(a) { return a.charCodeAt(0); }
  4.  
  5. encodeBtn = document.querySelector("#main > div:nth-child(1) > button");
  6.  
  7. dencodeBtn = document.querySelector("#main > div:nth-child(2) > button");
  8.  
  9. encodeBtn.addEventListener('click', encodeMessages);
  10.  
  11. dencodeBtn.addEventListener('click', decodeMessages);
  12.  
  13. function encodeMessages() {
  14.  
  15. let theText = document.querySelector("#main > div:nth-child(1) > textarea").value;
  16.  
  17. let encodedMessage = '';
  18.  
  19.  
  20. for (let i = 0; i < theText.length; i++) {
  21.  
  22. encodedMessage += String.fromCharCode(ascii(`${theText[i]}`) + 1);
  23. }
  24.  
  25. document.querySelector("#main > div:nth-child(1) > textarea").value="";
  26.  
  27. document.querySelector("#main > div:nth-child(2) > textarea").value = encodedMessage ;
  28.  
  29. }
  30.  
  31.  
  32. function decodeMessages() {
  33.  
  34. let theText = document.querySelector("#main > div:nth-child(2) > textarea").value;
  35.  
  36.  
  37. let decodedMessage = '';
  38.  
  39. for (let i = 0; i < theText.length; i++) {
  40.  
  41. decodedMessage += String.fromCharCode(ascii(`${theText[i]}`) - 1);
  42. }
  43.  
  44. document.querySelector("#main > div:nth-child(2) > textarea").value = decodedMessage ;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement