Look-and-say sequence: Difference between revisions

Line 2,339:
 
=={{header|JavaScript}}==
=== With RegExp ===
{{trans|Perl}}
<lang javascript>function lookandsay(str) {
Line 2,350 ⟶ 2,351:
}</lang>
 
=== Imperative version ===
Without RegExp
 
<lang javascript>function lookAndSay( s="" ){
var tokens=[]
var i=0, j=1
while( i<s.length ) {
while( j<s.length && s[j]===s[i] ) j++
j++
tokens.push( `${j-i}${s[i]}` )
i=j++
Line 2,363 ⟶ 2,362:
return tokens.join("")
}
 
 
for(let n=0,phrase="1"; n<10; n++ )
Anonymous user