Advertisement
Guest User

GPE_SuperArray - meant to sort and do cool things with array

a guest
Jan 15th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. console.log("Initiating GPE Super Array extension.");
  2. Array.prototype.at = function(atPos)
  3. {
  4.     if( atPos < this.length )
  5.     {
  6.         return this[atPos];
  7.     }
  8.     return -1;
  9. }
  10.  
  11. Array.prototype.makeLowercase = function()
  12. {
  13.     for (var i = 0; i < this.length; i++)
  14.     {
  15.         if (typeof this[i] === 'string')
  16.         {
  17.              this[i] = this[i].toLowerCase();
  18.          }
  19.      }
  20. }
  21.  
  22.  
  23. Array.prototype.makeUppercase = function()
  24. {
  25.     for (var i = 0; i < this.length; i++)
  26.     {
  27.         if (typeof this[i] === 'string')
  28.         {
  29.              this[i] = this[i].toUpperCase();
  30.          }
  31.      }
  32. }
  33.  
  34. Array.prototype.floorValues = function()
  35. {
  36.     for (var i = 0; i < this.length; i++)
  37.     {
  38.         if (typeof this[i] === 'number')
  39.         {
  40.              this[i] = Math.floor(this[i]);
  41.          }
  42.      }
  43. }
  44.  
  45. Array.prototype.roundValues = function()
  46. {
  47.     for (var i = 0; i < this.length; i++)
  48.     {
  49.         if (typeof this[i] === 'number')
  50.         {
  51.              this[i] = Math.round(this[i]);
  52.          }
  53.      }
  54. }
  55.  
  56. Array.prototype.roundByDecimals = function(decimalPlaces)
  57. {
  58.     var roundByTens = 0;
  59.     decimalPlaces = Math.floor(decimalPlaces);
  60.     if (decimalPlaces >=0 )
  61.     {
  62.         roundByTens = Math.pow(10,decimalPlaces);
  63.         for (var i = 0; i < this.length; i++)
  64.         {
  65.             if (typeof this[i] === 'number')
  66.             {
  67.                  this[i] = Math.round(this[i]/roundByTens)* roundByTens;
  68.              }
  69.          }
  70.     }
  71.     else
  72.     {
  73.          //handle negative decimal places(.1, .01, .001, etc, etc)
  74.         for (var i = 0; i < this.length; i++)
  75.         {
  76.             roundByTens = Math.pow(10,-1*decimalPlaces);
  77.             if (typeof this[i] === 'number')
  78.             {
  79.                 this[i] = roundedBeforeDecimal+ (Math.round(this[i]*roundByTens)/roundByTens);
  80.              }
  81.          }
  82.     }
  83. }
  84.  
  85. Array.prototype.ceilValues = function()
  86. {
  87.     for (var i = 0; i < this.length; i++)
  88.     {
  89.         if (typeof this[i] === 'number')
  90.         {
  91.              // this is a string
  92.              this[i] = Math.ceil(this[i]);
  93.          }
  94.      }
  95. }
  96.  
  97. Array.prototype.size = function()
  98. {
  99.      return this.length;
  100. }
  101.  
  102. //emulates the push_back push_front
  103. Array.prototype.push_back = function(inValue)
  104. {
  105.      this.push(inValue);
  106. }
  107.  
  108.  
  109. Array.prototype.pop_back = function()
  110. {
  111.      return this.pop();
  112. }
  113.  
  114. console.log("GPE Super Array loaded");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement