Last active
May 10, 2022 06:23
-
-
Save fazeelanizam13/6e07ae8647375e4dfd282e441b94d841 to your computer and use it in GitHub Desktop.
This file contains 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
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