Javascript String repeatify(times)

Description

Javascript String repeatify(times)


String.prototype.repeatify = String.prototype.repeatify || function(times) {
   var str = '';
   for (var i = 0; i < times; i++) {
      str += this;//  w  ww.j  a  v a  2 s  .c om
   }
   return str;
};

console.log('hello'.repeatify(3));

Javascript String repeatify(times)

/**//from  w  ww  .  j  ava  2s. c o m
 * JavaScript objects only have one construct, objects. Each object has an internal link to another object called its Prototype. This Prototype object has a Prototype of its own, and so on until an object is reached with a null as its Prototype.
 * - this is called the Prototype chain
 *
 * Extending natice prototypes: this is considered bad practice and breaks encapsulation. The only good reason for extending a built-in prototype is to backport the features of newer JavaScript engines.
 */

String.prototype.repeatify = String.prototype.repeatify || function(times) {
    var string = '';
    for (var i = 0; i < times; i++) {
        string += this;
    }

    return string;
}

console.log('Hello'.repeatify(5));

Javascript String repeatify(times)

// Basic alert//from   w w w .  j  a va 2 s .co  m
// alert("hello from Me");


// Write something to web page
// document.write("Hello Boulder");


// Variables
// var nameList = 100;
//
//   alert(nameList);

// Arrays
// var arrayName = new Array(10, 20, 40, 4000, 200, "Dude!");
//
// alert(arrayName[3]);
// alert(arrayName[1]);
// alert(arrayName[0]);
// alert(arrayName[5]);

// Define a repeatify function on the String object. The function accepts an integer
// that specifies how many times the string has to be repeated. The function returns
// the string repeated the number of times specified. For example: console.log('hello'.repeatify(3));

String.prototype.repeatify = String.prototype.repeatify || function(times){
  var str = '';

  for(var i=0; i<times; i++){
    str +=this;
  }
  return str;
};

console.log('hello'.repeatify(3));



PreviousNext

Related