Javascript String odd()

Description

Javascript String odd()


// Array.prototype.slice works with strings - works like .split('')
// reduce is called with index as third operator
String.prototype.odd = function () {
 return Array.prototype.slice.call(this)
  .reduce((s, e, i) => i % 2 ? s + e : s)
}

Javascript String odd()

String.prototype.odd = function() {
  var temp = [];//from   ww  w  .j  ava  2s.co  m
  for (var i = 0; i < this.length; i++) {
    if (i%2 == 0) continue;
    temp.push(this[i]);
  };
  return temp.join('');
};



PreviousNext

Related