Advertisement
Lulunga

Strings and RegExp 01. Binary Decoding

Oct 21st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let input = document.getElementById("input").value;
  3.     let sum = calculateSum(input);
  4.     let result = document.getElementById('resultOutput');
  5.  
  6.     function calculateSum(input) {
  7.         return input.split('').map(Number).reduce((a, b) => a + b);
  8.     }
  9.     if (String(sum).length > 1) {
  10.         while (String(sum).length > 1) {
  11.             sum = String(sum).split('').map(Number).reduce((a, b) => a + b);
  12.         }
  13.     }
  14.     result.innerHTML = input.substring(sum, input.length - sum).match(new RegExp('.'.repeat(8), "gim"))
  15.         .map(e => parseInt(e, 2)).map(e => String.fromCharCode(e)).join('').match(/[a-zA-Z ]+/gim).join('');
  16.     return result;
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement