Advertisement
Lulunga

Strings and RegExp 1. Binary Decoding

Oct 21st, 2019
152
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
  8.             .split('')
  9.             .map(Number)
  10.             .reduce((a, b) => a + b);
  11.     }
  12.     if (String(sum).length > 1) {
  13.         while (String(sum).length > 1) {
  14.             sum = String(sum)
  15.                 .split('')
  16.                 .map(Number)
  17.                 .reduce((a, b) => a + b);
  18.         }
  19.     }
  20.     result.innerHTML = input
  21.         .substring(sum, input.length - sum)
  22.         .match(new RegExp('.'.repeat(8), "gim"))
  23.         .map(e => parseInt(e, 2))
  24.         .map(e => String.fromCharCode(e)).join('');
  25.     return result;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement