Advertisement
dimipan80

Exams - Double Rakiya Numbers

Nov 17th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* A "double rakiya number" is an integer that contains a sequence of 2 digits twice (without overlapping).
  2.  For example "23156312" is a "double rakiya number" because it contains "31" twice. Other examples
  3.  of "double rakiya numbers" are: 1212, 3333, 203103, 5210217, 21212121, and 55555. Examples
  4.  of non-"double rakiya numbers" are: 333, 5, 111222, 1234567131, and 12213114.
  5.  Write a JavaScript function that takes as input two numbers (start and end) and prints at the console
  6.  a HTML list holding all numbers in the range [start…end], along with a link to view details about
  7.  all "double-rakiya numbers" in that range. */
  8.  
  9. "use strict";
  10.  
  11. function printHTMLnumberList(arr) {
  12.     var start = Number(args[0]);
  13.     var end = Number(args[1]);
  14.  
  15.     function checkRepeatingTwoDigits(number) {
  16.         if (number > 1009) {
  17.             var numStr = number.toString(10);
  18.             for (var i = 0; i + 3 < numStr.length; i += 1) {
  19.                 for (var j = i + 2; j + 1 < numStr.length; j += 1) {
  20.                     if (numStr[j] == numStr[i] && numStr[j + 1] == numStr[i + 1]) {
  21.                         return true;
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.         return false;
  27.     }
  28.  
  29.     args[0] = '<ul>';
  30.     var index = 1;
  31.     for (var i = start; i <= end; i += 1) {
  32.         args[index] = "<li><span class=";
  33.         var klass = "'num'>";
  34.         var href = '';
  35.         if (checkRepeatingTwoDigits(i)) {
  36.             klass = "'rakiya'>";
  37.             href = '<a href="view.php?id=' + i + '>View</a>';
  38.         }
  39.  
  40.         args[index] += klass + i + "</span>" + href + "</li>";
  41.         index++;
  42.     }
  43.  
  44.     args[index] = "</ul>";
  45.     console.log(args.join('\n'));
  46. }
  47.  
  48. printHTMLnumberList(['5', '8']);
  49. printHTMLnumberList(['11210', '11215']);
  50. printHTMLnumberList(['55555', '55560']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement