Find the nth term in Look-and-say (Or Count and Say) Sequence. The look-and-say sequence is the sequence of below integers: 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, …

a

sequenceof digits (also calledLook-and-Say) where each term is made of the reading of the digits of the previous term.Check this out to learn more about Look and Say Sequence.

So if you have given input “3”, it should print 1 11 21 1211.

**Read More About Another String Data Type Coding Interview Questions and Answers.**

## Look and Say Coding Interview Question

*Sample Data:*

Input: 2

Output: 1 11 21

Input: 3

Output: 1 11 21 1211

Input: 4

Output: 1 11 21 1211 111221

So We will start with “1” and we write next One one (“11”), then we can see two ones so it will be “21”, and then we see one two one one so we will write 1211 and so on.

Now as we have clarified what exactly we need to add as logic we can start building pseudo code.

For first step, its easy we can say “1” + num,

Now we need to check string length, its greater than 1 then we need to see if its repeated characters then we will just add counter.

For 11, we check if second character is same as previous one then we need to add counter, if it different then we will return “1” + currentcharacter.

*So we have three Conditions to check to printout Output.*

First Condition:

If input string length is less than 2 => return “1” + current input string.

Add For loop for string length > 2

**Second Condition:**

If current character is same as previous character => return counter + current character.

**Third Condition:**

If current character is not same as previous character => return “1” + current character.

```
const lookSay = (input) => {
//Check input value.
//Also make input data type to string
input = ''+input;
if (input.length === 1) {
return '1' + input;
}
//console.log(input);
let out = '';
let cur = input[0];
let count = 1;
for (let i = 1; i <= input.length; i++) {
if (cur !== input[i] || i === input.length) {
out += `${count}${cur}`;
cur = input[i];
count = 1;
}
else {
count++;
}
}
//console.log(out);
return out;
}
```

*Print OutPut Using Function Block*

```
const printOutput(num = 0, out = '') {
let val = 1;
out = val;
for (let i = 1; i <= num; i++) {
val = out_to_val(val);
out += ' ' + val;
}
}
```*let out;
console.log(printOutput(4, out));*

You can also create different test edge cases to verify your code.