Warm Up · Star Pattern
Right-Angled Triangle of Alternating 1s and 0s
Problem Statement
Write a program that prints a triangle of alternating 1s and 0s, starting with 1 on each row.
Output
1 10 101 1010
Approach
- Outer loop (Rows): Loop
ifrom0ton - 1. - Initialize toggle = 1: Start each row with
toggle = 1. - Inner loop (Columns): For each row, loop
jfrom0toi. On each iteration, appendtoggleto the row string, then fliptogglebetween 1 and 0. - Print Row: After the inner loop, print the row string.
Time & Space Complexity
Time Complexity: O(n^2)
Space Complexity: O(n) per row.
Your solution
Solve it in JavaScript and print the answer with console.log(). Run to see your output, then Submit to check it against the test cases.
// Write your solution in JavaScript // Print the answer with console.log()
Console
No output yet. Use console.log() to print values here.
Write your own solution and press Submit once to unlock the answer.