Advertisement
dimipan80

Exams - Numerology (on JavaScript)

Dec 30th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Numerology involves a lot of repeated calculations, but as a programmer you can automate this process and
  2.  earn some easy cash! You will be given the birthdate and username of a random fellow student.
  3.  Your task is to calculate a celestial number. Below is a description of the process, see the example
  4.  to understand your task better. First, multiply together the numbers representing the day, year and month
  5.  of the birthdate. Numerologists love odd numbers, so if the month is an odd number, you should square the result
  6.  of the multiplication. Next, add to the result each digit ('0' = 0, '1' = 1… '9' = 9) or the position in
  7.  the English alphabet of each letter in the username – e.g. “a” = 1, “b” = 2… “z” = 26.
  8.  Capital letters weigh twice as much - the letter “A” will add 1*2 to the sum, “Z” will add 2*26, etc.
  9.  13 is a sacral number and your celestial number should be between 0 and 13 inclusive.
  10.  So, if the resulting number is greater than 13 you should keep adding its digits together until you get
  11.  the coveted celestial number in its final form. Then all you have to do is print it to the console!
  12.  On the only input line you will be given a date in the format [day.month.year] and a username,
  13.  separated by a single space. On the only output line you must print the calculated celestial number. */
  14.  
  15. "use strict";
  16.  
  17. function solve(args) {
  18.     args = args[0].split(/\W+/).filter(Boolean);
  19.     var username = args.pop();
  20.     var result = 1;
  21.     var i;
  22.     for (i = 0; i < args.length; i += 1) {
  23.         result *= parseInt(args[i]);
  24.     }
  25.    
  26.     if ((parseInt(args[1]) % 2) != 0) {
  27.         result *= result;
  28.     }
  29.    
  30.     for (i = 0; i < username.length; i += 1) {
  31.         var symbol = username[i];
  32.         if (isFinite(symbol)) {
  33.             result += parseInt(symbol);
  34.         } else {
  35.             var weighCoeffic = 1;
  36.             if (symbol != symbol.toLowerCase()) {
  37.                 weighCoeffic = 2;
  38.             }
  39.             symbol = symbol.toLowerCase();
  40.             result += weighCoeffic * (symbol.charCodeAt(0) - 96);
  41.         }
  42.     }
  43.    
  44.     while (result > 13) {
  45.         var digitsStr = result.toString(10);
  46.         result = 0;
  47.         for (i = 0; i < digitsStr.length; i += 1) {
  48.             result += parseInt(digitsStr[i]);
  49.         }
  50.     }
  51.    
  52.     console.log(result);
  53. }
  54.  
  55. solve(['14.03.1990 Panayot']);
  56. solve(['01.01.1914 g0g0']);
  57. solve(['25.05.1997 P360']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement