Advertisement
vvsvvs

Untitled

Oct 1st, 2022
772
0
117 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function encodeAndDecodeMessages() {
  2.  
  3.     function encodeDecodeMessage(message, step){
  4.         let s = '';
  5.  
  6.         for (let index = 0; index < message.length; index++) {
  7.             s += String.fromCharCode(message.charCodeAt(index) + step);            
  8.         }
  9.  
  10.         return s;
  11.     }
  12.  
  13.     let mainElement = document.getElementById('main');
  14.  
  15.     let textAreaElements = mainElement.getElementsByTagName('textarea');
  16.     let messageTextArea = textAreaElements[0];
  17.     let receivedMessageTextArea = textAreaElements[1];
  18.  
  19.     function mouseClick(e){
  20.         let targetElement = e.target;
  21.         let parentElement = targetElement.parentNode;
  22.         let textAreaElement = parentElement.getElementsByTagName('textarea')[0];
  23.        
  24.  
  25.         if (targetElement.tagName = 'BUTTON') {
  26.             if (targetElement.textContent == 'Encode and send it') {
  27.                 let message = messageTextArea.value;
  28.                 messageTextArea.value = '';
  29.  
  30.                 if (message != '') {
  31.                     receivedMessageTextArea.value = encodeDecodeMessage(message, 1);                    
  32.                 }
  33.  
  34.             } else if (targetElement.textContent == 'Decode and read it') {
  35.                 let codedMessage = receivedMessageTextArea.value;
  36.  
  37.                 receivedMessageTextArea.value = encodeDecodeMessage(codedMessage, -1);
  38.             }        
  39.         }
  40.     }
  41.    
  42.     mainElement.addEventListener('click', mouseClick);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement