Advertisement
waltermilner

Untitled

Jan 27th, 2022
925
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.  
  5. <head>
  6.  
  7.     <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  8.  
  9.     <script>
  10.         'use strict';
  11.         var data = [30, 28, 2, 12, 4, 20, 8, 18, 10];
  12.         // sort defaults to alphabetic sort of strings
  13.         // we have to provide a compare function
  14.         data = data.sort(function (a, b) {
  15.             return a - b
  16.         });
  17.         // speed depends on JS implementation
  18.         console.log(data);
  19.  
  20.         var minRange = 0;
  21.         // find minimum range
  22.         var notFound = true;
  23.         while (notFound) {
  24.             for (var index = 0; index < data.length - 1; index++) {
  25.                 if (data[index + 1] === data[index] + minRange) {
  26.                     notFound = false;
  27.                     break;
  28.                 }
  29.             }
  30.             if (!notFound) break;
  31.             minRange++;
  32.         }
  33.         console.log("Min range = ", minRange);
  34.         // output all minRange pairs
  35.         for (var index = 0; index < data.length - 1; index++) {
  36.             if (data[index + 1] == data[index] + minRange) {
  37.                 console.log(data[index], data[index + 1]);
  38.             }
  39.         }
  40.     </script>
  41.  
  42.  
  43.  
  44. </head>
  45.  
  46. <body>
  47.  
  48. </body>
  49.  
  50. </html>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement