문장에서 특정 문자가 몇 번 나타나는지 찾는 JavaScript 함수를 작성해야 합니다.
예시
이에 대한 코드는 -
const string = 'This is just an example string for the program';
const countAppearances = (str, char) => {
let count = 0;
for(let i = 0; i < str.length; i++){
if(str[i] !== char){
// using continue to move to next iteration
continue;
};
// if we reached here it means that str[i] and char are same
// so we increase the count
count++;
};
return count;
};
console.log(countAppearances(string, 'a'));
console.log(countAppearances(string, 'e'));
console.log(countAppearances(string, 's')); 출력
콘솔의 출력 -
3 3 4