Skip to content

Files

Latest commit

 

History

History
29 lines (20 loc) · 747 Bytes

count-characters-in-your-string.md

File metadata and controls

29 lines (20 loc) · 747 Bytes

Count characters in your string 6 Kyu

LINK TO THE KATA - STRINGS FUNDAMENTALS

Description

The main idea is to count all the occurring characters in a string. If you have a string like aba, then the result should be {'a': 2, 'b': 1}.

What if the string is empty? Then the result should be empty object literal, {}.

Solution

const count = string => {
  const result = {}

  for (let i = 0; i < string.length; i++) {
    const character = string.charAt(i)

    result[character] ? result[character]++ : (result[character] = 1)
  }

  return result
}