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:
- Seed Term: "1"
- First Term: One "1" - "11"
- Second Term: Two "1"s - "21"
- Third Term: One "2" followed by one "1" - "1211"
- Fourth Term: One "1," one "2," and two "1"s - "111221"
- Fifth Term: Three "1"s, two "2"s, and one "1" - "312211"
- ... 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
MediumThe 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
1Introduction
What is here for you?