Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fazeelanizam13/6e07ae8647375e4dfd282e441b94d841 to your computer and use it in GitHub Desktop.
Save fazeelanizam13/6e07ae8647375e4dfd282e441b94d841 to your computer and use it in GitHub Desktop.
function countCharOccurances (text, character) {
// convert input to string and split the string into an array of charaters
let chars = String(text).split("")
// if doesn't include, return 0
if (!chars.includes(String(character))) {
return 0
} else {
let count = 0
// else, iterate through each chatacter
for (let i = 0; i < chars.length; i++) {
// if matches given character, increase count
if (chars[i] === String(character)) count = count + 1
}
// return count at the end
return count
}
}
console.log(countCharOccurances('cat', 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment