Javascript String substrings()

Description

Javascript String substrings()


a = "superbowl"/*from w w  w. j a  v a 2s  .  com*/

String.prototype.substrings = function () {
  subs = [];

  for(var i = 0; i < this.length; i++) {
    for( var j = i+1; j < this.length+1; j++) {
      subs.push(this.slice(i,j));
    };
  };

  return subs;
};

console.log(a.substrings());

Javascript String substrings()

String.prototype.substrings = function(){
  var substrings = []
  for (var i=0; i<this.length; i++){
    for (var j=i+1; j<=this.length; j++){
      substrings.push(this.slice(i,j));/*from w w w  .  j  a va2  s.  c o  m*/
    }
  }
  return substrings;
}

var str = 'cat';
console.log(str.substrings());

Javascript String substrings()

String.prototype.substrings = function () {
  let resultArr = [];

  for (var i = 0; i < this.length; i++) {
    for (var j = i + 1; j <= this.length; j++) {
      resultArr.push(this.slice(i, j));//from   w  ww  .j a  v a2 s .  c  om
    }
  }

  return resultArr;
};

console.log("cat".substrings());

Javascript String substrings()

String.prototype.substrings = function () {
  var answer = [];
  for(var i = 0; i < this.length; i++) {
    for(var j = i + 1; j < (this.length + 1); j++) {
      var sub = this.slice(i, j);
      if (answer.indexOf(sub) == -1) {
        answer.push(sub);//ww w .  j a  v a  2 s.  co m
      }
    }
  }
  return answer;
}

Javascript String subStrings()

String.prototype.subStrings = function () {
  var substrings = [];

  for (var i = 0; i < this.length ; i++) {
    for (var j = i+1; j <= this.length; j++) {
      substrings.push(this.slice(i,j));//from   w w  w .jav a 2s  .co m
    };
  };

  return substrings;
};

Javascript String substrings()

String.prototype.substrings = function() {
  let subs = [];//from w w w.  j  a  va2 s .  c  o m
  for (let i = 0; i < this.length; i++){
    for (let j = 0; j < this.length; j++){
      let sub = this.slice(i,j);
      if (subs.indexOf(sub) === -1){
        subs.push(sub)
      }
    }
  }

  return subs;
};

Javascript String substrings()

String.prototype.substrings = function() {
  let substrings = [];

  for (let i = 0; i < this.length; i++) {
    for (let j = i + 1; j < this.length + 1; j++) {
      substrings.push(this.slice(i, j));
    }/*from ww w .j a  v a 2  s  . com*/
  }

  return substrings;
}

Javascript String substrings()

function bubbleSort(ary) {
  var sorted = false;
  while(!sorted) {
    sorted = true// w  ww .j av a 2  s .  c  o  m
    for (var i = 0; i < ary.length -1; i++) {
        if(ary[i] > ary[i+1]) {
          var temp = ary[i+1];
          ary[i+1] = ary[i];
          ary[i] = temp;
          sorted = false;
        }
      }
    }
    return ary;
}

String.prototype.substrings = function() {
  var result = [];
  for (var i = 0; i < this.length; i++) {
    for (var j = i+1; j < this.length+1; j++) {
      var substring = this.slice(i, j);
      if (result.indexOf(substring) === -1){
        result.push(substring);
      }
    }
  }
  return result;
};

Javascript String substrings()

String.prototype.substrings = function () {
    const res = [];//from w w  w .  j a  va 2s  .co m
    for (let i = 0; i < this.length; i++) {
      for (let j = i + 1; j <= this.length; j++) {
        let word = this.slice(i,j);
        if (!res.includes(word)){
          res.push(word);
        }
      }
    }

    return res;
};

Javascript String subStrings()

String.prototype.subStrings = function() {
  var result = [];
  for (var i = 0; i < this.length; i++) {
    for (var j = i + 1; j <= this.length; j++) {
      result.push(this.slice(i,j));/*  w  w w . j  a v a  2  s  .c  o m*/
    }
  }
  return result;
};

Javascript String substrings()

String.prototype.substrings = function() {
  var sub_strs = [];
  for (var x = 0; x < this.length; x++){
    for (var y = (x + 1); y <= this.length; y++){
      sub_strs.push(this.slice(x,y));//from w ww .j a  v a 2s  . c o m

    }
  }
  return sub_strs;
};

Javascript String substrings()

String.prototype.substrings = function () {
  const substrings = [];/*from   w w  w  .ja v a2 s  .  com*/
  for (let i = 0; i < this.length; i++) {
    for (let j = i+1; j <= this.length; j++) {
      substrings.push(this.slice(i,j));
    }
  }

  return substrings;
};

console.log("cat".substrings());

Javascript String substrings()

String.prototype.substrings = function() {
  subs = [];/*from w ww  .j  a va 2 s .  c o m*/

  for(i = 0; i < this.length; i++) {
    for(j = i + 1; j <= this.length; j++) {
      if (subs.indexOf(this.substring(i, j)) === -1) {
        subs.push(this.substring(i, j));
      }
    }
  }

  return subs;
}

console.log('Caat'.substrings())

Javascript String substrings()

String.prototype.substrings = function() {
  let substrings = [];
  for (let i = 0; i < this.length; i++) {
    for(let j = i + 1; j <= this.length; j++){
      substrings.push((this.slice(i, j)));
    }/*from  ww w  .j  a  va  2 s  .  c  o m*/
  }
  return substrings;
};

console.log("under pressure".substrings());

Javascript String subStrings()

String.prototype.subStrings = function () {
  var subs = [];/*from   w ww.  j  a v a 2 s .  co m*/

  for (var i = 0; i < this.length; i++) {
    for (var j = i+1; j <= this.length; j++) {
      subs.push(this.slice(i,j));
    };
  };

  var sorted = subs.sort();
  var uniques = [sorted[0]];

  for(var i = 1; i < sorted.length; i++) {
    if(sorted[i] !== sorted[i-1]) {
      uniques.push(sorted[i]);
    };
  };

  return uniques;
};

Array.prototype.bubbleSort = function () {
  for (var i = 0; i < this.length; i++) {
    for (var j = i+1; j < this.length; j++) {
      if (this[i] > this[j]) {
        var dummy = this[i];
        this[i] = this[j];
        this[j] = dummy;
      };
    };
  };

  return this;
};

Javascript String substrings()

String.prototype.substrings = function () {
  let result = [];
  let strArr = this.split("");

  for (i = 0; i < strArr.length; i++) {
    for (j = i+1; j < strArr.length+1; j++) {
      result.push(strArr.slice(i, j).join(""));
    }/*from  ww  w .ja  v  a 2 s.c o  m*/
  }

  return result;
}

let fruits = 'apples';
console.log(fruits.substrings());

Javascript String substrings()

String.prototype.substrings = function () {

  var subs = [];/*from  w ww  .j  a va 2s . co m*/

  for (var i = 0; i < this.length; i++) {

    if (subs.indexOf(this[i]) === -1) {
      subs.push(this[i]);
    }

    for (var j = i+1; j < this.length + 1; j++) {
      var sliced = this.slice(i,j+1);
      if (subs.indexOf(sliced) === -1) {
        subs.push(sliced);
      }
    }
  }
  return subs;
}



PreviousNext

Related