Menu
Coddy logo textTech

30. Count And Say

Lesson 31 of 31 in Coddy's 30 Days of Logic Building in Javascript course.

The "count and say" sequence is a series of numbers where each term describes the count of consecutive digits in the previous term. It starts with the seed term "1" and continues as follows:

  1. Seed Term: "1"
  2. First Term: One "1" - "11"
  3. Second Term: Two "1"s - "21"
  4. Third Term: One "2" followed by one "1" - "1211"
  5. Fourth Term: One "1," one "2," and two "1"s - "111221"
  6. Fifth Term: Three "1"s, two "2"s, and one "1" - "312211"
  7. ... and so on.

Each term is formed by reading the previous term and counting the consecutive occurrences of each digit. The sequence exhibits a pattern of how numbers are verbally described, hence the name "count and say" sequence.

challenge icon

Challenge

Medium

The count-and-say sequence is a series of strings where each string is generated based on the previous string. Write a function to generate the nth string in the count-and-say sequence.

  • Input: <strong>4</strong>
  • Expected Output: <strong>"1211"</strong>

Try it yourself

function countAndSay(n) {
    // write your code below 
    
}

All lessons in 30 Days of Logic Building in Javascript