Skip to content

Instantly share code, notes, and snippets.

@harryadel
Created August 26, 2016 19:34
Show Gist options
  • Save harryadel/01c061a86fb98fddec77861540f735d7 to your computer and use it in GitHub Desktop.
Save harryadel/01c061a86fb98fddec77861540f735d7 to your computer and use it in GitHub Desktop.
/*
My answer for exercise 'Bean counting'
in Eloquent JavaScript Second Edition
Chapter 3 Functions
*/
function countBs(string) {
var str = string.toLowerCase();
var array = str.split("");
var arrayLength = array.length;
var num = 0;
for(var i = 0; i < arrayLength; i++) {
if(array[i] == "b") {
num ++;
}
}
return num
}
/*
I copied everthing I did above, and
changed the "b" to "letter" instead,
which is passed as the argument
*/
function countChar(string,letter) {
var str = string.toLowerCase();
var array = str.split("");
var arrayLength = array.length;
var num = 0;
for(var i = 0; i < arrayLength; i++) {
if(array[i] == letter) {
num ++;
}
}
return num
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment