svetlozar_kirkov

Numbers (JS Exam)

Apr 3rd, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     var inputString = args[0];
  3.     var pattern = /\d+/g;
  4.     var results = [];
  5.     while ((arr = pattern.exec(inputString)) !== null) {
  6.             results.push(arr[0]);
  7.     }
  8.     var collection = [];
  9.     for (var i in results) {
  10.         var tempNum = parseInt(results[i]);
  11.         var tempNumInHex = tempNum.toString(16).toUpperCase();
  12.         if (tempNumInHex.length < 4) {
  13.             var tempArr = [];
  14.             for (var i = 0; i < 4 - tempNumInHex.length; i = i+1) {
  15.                 tempArr.push('0');
  16.             }
  17.             tempArr.push(tempNumInHex);
  18.             var final = tempArr.join('');
  19.             collection.push('0x' + final);
  20.         }
  21.         else {
  22.             collection.push('0x' + tempNumInHex);
  23.         }
  24.     }
  25.     var finishedString = collection.join('-');
  26.     console.log(finishedString);
  27. }
  28.  
  29. /*solve(['5tffwj(//*7837xzc2---34rlxXP%$”']);
  30. solve(['482vMWo(*&^%$213;k!@41341((()&^>><///]42344p;e312']);
  31. solve(['20']);*/
Advertisement
Add Comment
Please, Sign In to add comment