Javascript String countAll(letter)

Introduction

Javascript String countAll(letter)

String.prototype.countAll = function (letter) {
  var letterCount = 0

  for (var i = 0; i < this.length; i++) {
    if (this.charAt(i).toUpperCase() == letter.toUpperCase()){
      letterCount++/*  w w w  .j a v  a2  s .  com*/
    }
  }

  return letterCount
}

var sen = 'Come on, get up and fight'

console.log(sen.countAll('o'))

Javascript String countAll(letter)

// objects have already defined properties

// valueOf//www  .j a  v  a 2  s.  c  o  m
// constructor
// toLocaleString
// toString
// isPrototypeOf
// propertyIsEnumerable
// hasOwnProperty

// all of the above properties belong to object prototypes
// A Prototype is like a blueprint object for the object we are trying to create.
// Passing down properties is called inheritence.


String.prototype.countAll = function(letter){
  var letterCount = 0;
  for(var i=0; i<=this.length; i++){
    if(this.charAt(i).toUpperCase() == letter.toUpperCase() ){
      letterCount++;
    }
  }
  return letterCount;
};



// Creating objects using object.create()



PreviousNext

Related