Question: Write and test a function named countOccurrences that accepts two arguments (a string and a character) and counts the number of times the character appears
Write and test a function named countOccurrences that accepts two arguments (a string and a character) and counts the number of times the character appears in the string.
Note that JavaScript does not have a character data type. Characters in JavaScript are just strings of length 1.
Examples (case-sensitive):
countOccurrences(TBH, JavaScript is not boring!, e) => 0
countOccurrences(TBH, JavaScript is not boring!, a) => 2
countOccurrences(TBH, JavaScript is not boring!, T) => 1
countOccurrences(The albatross just ate your lunch!!, !) =>2
Code should look kinda like this:
var countEcho = function(s) {
//initialize count
count = 0;
//count e's in s
for (var i = 0; i < s.length; ++i)
if (s.charAt(i) === "e")
++count;
//return the result
return count;
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
