Advertisement
dimipan80

Exams - The Numbers

Nov 14th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Your job is to clear the text from any unnecessary symbols (only the numbers are needed) and convert
  2. the remaining number sequences to hex format. If a hex value has less than 4 characters, you need to add
  3. leading zeros. Finally, you need to place a "0x" prefix before each hex value and concatenate them all with
  4. dashes '-'. The input data will be received as an array. It contains one argument – the initial message
  5. you need to transform. The output consists of only one line – the transformed message. */
  6.  
  7. function transformMessage(arr) {
  8.     var numbers = arr[0].split(/[^0-9]+/g).filter(Boolean);
  9.     for (var i = 0; i < numbers.length; i += 1) {
  10.         var hex = Number(numbers[i]).toString(16).toUpperCase();
  11.         while (hex.length < 4) {
  12.             hex = '0'.concat(hex);
  13.         }
  14.         numbers[i] = '0x'.concat(hex);
  15.     }
  16.  
  17.     return numbers.join('-');
  18. }
  19.  
  20. console.log(transformMessage(['5tffwj(//*7837xzc2---34rlxXP%$”.']));
  21. console.log(transformMessage(['482vMWo(*&^%$213;k!@41341((()&^>><///]42344p;e312']));
  22. console.log(transformMessage(['20']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement