Created
August 26, 2016 19:34
-
-
Save harryadel/01c061a86fb98fddec77861540f735d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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