Skip to content

Instantly share code, notes, and snippets.

@abdulhalim-cu
Created October 25, 2017 12:34
Show Gist options
  • Select an option

  • Save abdulhalim-cu/8df7aaf0df74d40e21b82a2c3121ba30 to your computer and use it in GitHub Desktop.

Select an option

Save abdulhalim-cu/8df7aaf0df74d40e21b82a2c3121ba30 to your computer and use it in GitHub Desktop.
You can get the Nth character, or letter, from a string by writing "string".charAt(N), similar to how you get its length with "s".length. The returned value will be a string containing only one character (for example, "b"). The first character has position zero, which causes the last one to be found at position string.length - 1. In other words,…
// Your code here.
function countBs(string){
number_of_bs = 0;
for (count = 0; count < string.length; count++){
if (string[count] == "B")
number_of_bs += 1;
}
return number_of_bs;
}
function countChar(string, char){
number_of_char = 0;
for (count = 0; count < string.length; count++){
if (string[count] == char)
number_of_char += 1;
}
return number_of_char;
}
console.log(countBs("BBC"));
// → 2
console.log(countChar("KKKKBCD", "K"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment