Nodejs Array to Twenty Convert toTwenty()

Here you can find the source of toTwenty()

Method Source Code

Array.prototype.toTwenty = function() {
  for(let i=1; i<=20; i++) {
    this.push(i);// w ww  .  j a va 2s  .co m
  }
  return this;
}

Array.prototype.toForty = function() {
  for(let i=2; i<=40; i+=2) {
    this.push(i);
  }
  return this;
}

Array.prototype.toOneThousand = function() {
  for(let i=10; i<=1000; i+=10) {
    this.push(i);
  }
  return this;
}

Array.prototype.search = function(num) {
  let count = 0;
  let startIndex = 0;
  let endIndex = this.length-1;

  while(startIndex <= endIndex){
    if (this[startIndex]===num) {
      return {count: count, index:startIndex, length: this.length}
    }
    if(this[endIndex]===num) {
      return {count: count, index:endIndex, length: this.length}
    }
    let midPoint = Math.floor((startIndex+endIndex)/2);
    if (this[midPoint] === num) {
      return {count: count, index: midPoint, length: this.length};
    }
    else if (this[midPoint] > num) {
      endIndex = midPoint-1;
      startIndex+=1;
    }
    else {
      startIndex = midPoint+1;
      endIndex-=1;
    }
    count+=1;
  }
  return {count: count, index: -1, length: this.length};
}

Related

  1. toTwenty()
    Array.prototype.toTwenty = function(){
      a = 1;
      b = [];
      for(i=1; i<=20; i++){
        b.push(i);
      return b
    
  2. toTwenty()
    'use strict';
    Array.prototype.toTwenty = function() {
      let arr = [];
      for (var i = 1; i <= 20; i++) {
        arr.push(i);
      return arr;
    Array.prototype.toForty = function() {
    ...
    
  3. toTwenty()
    Array.prototype.toTwenty = function(){
      for(var loop =1; loop<=20;loop++){
        this.push(loop);
      return this;
    };
    Array.prototype.toForty = function(){
      for(var loop = 1;loop<=20;loop++){
        this.push(loop*2);
    ...
    
  4. toTwenty()
    Array.prototype.toTwenty = function() {
        var arr = [];
        for (i = 0; i < 20; i++) {
            arr.push(i+1);
        return arr;
    Array.prototype.toForty = function() {
        var arr = [];
    ...
    
  5. toTwenty()
    Array.prototype.toTwenty = function(){
      var twenty = [];
      for ( var i = 1; i <= 20; i++){
        twenty.push(i)
          return twenty
    Array.prototype.toForty = function() {
      var forty = [];
    ...
    
  6. toTwenty()
    Array.prototype.toTwenty = function() {
      var newArray = [];
      for (var i = 1; i <= 20; i++) {
        newArray.push(i);
      return newArray;
    Array.prototype.toForty = function() {
      var newArray = [];
    ...
    
  7. toTwenty()
    Array.prototype.toTwenty = function(){
      for(var loop =1; loop<=20;loop++){
        this.push(loop);
      return this;
    };
    Array.prototype.toForty = function(){
      for(var loop = 1;loop<=20;loop++){
        this.push(loop*2);
    ...
    
  8. toTwenty()
    const binarySearch = function binarySearch (){
    Array.prototype.toTwenty = function () {
     for (var i = 1; i <= 20; i++){
       this.push(i);
     return this;
    };
    Array.prototype.toForty = function () {
     for (var i = 2; i <= 40; i+=2){
    ...
    
  9. toTwenty()
    Array.prototype.toTwenty = function(){
     var answer=[];
       for (var i=1; i<=20; i++){
         answer.push(i)
       return answer;
    Array.prototype.toForty = function(){
      var answer=[];
    ...