jQuery jAM, Season 6

Exercise 3.3

Bean Counting

Ok, So I took the long road again.

	 
function countBs(s){
  var count = 0;
 for(var i = 0; i < s.length; i++){
   if(s.charAt(i) == "B"){
     count++;
   }
}
   return count;
}

function countChar(s,x){
    var count = 0;
 for(var i = 0; i < s.length; i++){
   if(s.charAt(i) == x){
     count++;
   }
}
 return count;
}
console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → 4
	

Second Function

function countChar(string,char){
  var count = 0;
  var count_1 = 0;

 while(count < string.length)
  {
     if( string.charAt(count) == char)
       {
      	count_1= count_1+1;
        }
    count = count + 1;
  }
 return count_1;
}

console.log(countChar("kakkerlak", "k"));
// → 4
	 
	 

To open the JavaScript console, press F12 or on MAC press COMMAND-OPTION-I.