Find nth term of the Dragon Curve Sequence | function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output
function Dragon_Curve_Sequence ( n ) { let s = " " ; for ( let i = 2 ; i <= n ; i ++ ) { let temp = " " ; let prev = ' ' ; let zero = ' ' ; let one = ' ' ; for ( let j = 0 ; j < s . length ; j ++ ) { temp = temp + s [ j ] ; if ( prev == ' ' ) { temp += one ; prev = one ; } else { temp += zero ; prev = zero ; } } s = temp ; } return s ; } let n = 4 ; let s = Dragon_Curve_Sequence ( n ) ; document . write ( s + " " ) ;