/* Find the n’th 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, …
*/

function lookAndSay(n) {
  if (n == 1) return 1;
  const str = String(lookAndSay(n-1));
  let result = '';
  let count = 1;
  for (let i = 0; i < str.length; i++) {
    if (str[i] === str[i + 1]){
      count += 1;
    } else {
      result += count + str[i] + '';
      count = 1;
    }
  }

  return Number(result);
}

// ======= TEST ========
test();  // uncomment to test

function test() {

  const result1 = lookAndSay(3);
  console.log(result1, result1 === 21);

  const result2 = lookAndSay(5);
  console.log(result2, result2 === 111221);

  const result7 = lookAndSay(7);
  console.log(result7, result7 === 13112221);

}