본문 바로가기
알고리즘/Codewars

[Codewars] All Star Code Challenge #18 (8 kyu) / JavaScript

by fluss 2023. 3. 27.

https://www.codewars.com/kata/5865918c6b569962950002a1

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Create a function that accepts a string and a single character, and returns an integer of the count of occurrences the 2nd argument is found in the first one.

If no occurrences can be found, a count of 0 should be returned.

("Hello", "o")  ==>  1
("Hello", "l")  ==>  2
("", "z")       ==>  0
str_count("Hello", 'o'); // returns 1
str_count("Hello", 'l'); // returns 2
str_count("", 'z'); // returns 0

 

Notes

  • The first argument can be an empty string
  • In languages with no distinct character data type, the second argument will be a string of length 1

 

코드

function strCount(str, letter){  
  return str.split('').filter(el => el === letter).length;
}

댓글