알고리즘 문제
문자 개수 세기
3jun
2018. 10. 3. 23:12
나의 풀이
또 다른 풀이
// 주어진 단어(word)에 특정 알파벳(ch)이 몇 번 들어가는지 세어주는 함수 function countCharacter(word, ch) { var count = 0; for (var i = 0; i < word.length; i++) { if (word[i].toUpperCase() === ch.toUpperCase()) { count++; } } return count; }
// 주어진 단어(word)에 특정 알파벳(ch)이 몇 번 들어가는지 세어주는 함수 function countCharacter(word, ch) { var count = 0; for (var i = 0; i < word.length; i++) { if (word[i].toUpperCase() === ch.toUpperCase()) { count++; } } return count; } // 단어 word에 알파벳 'A'가 몇 번 나오는지 세어주는 함수 function countA(word) { return countCharacter(word, 'A'); } // 테스트 코드 console.log(countCharacter('AbaCedEA', 'E')); console.log(countCharacter('AbaCedEA', 'X')); console.log(countA('AbaCedEA'));