Solution Review: Pascal's Triangle
Learn to implement Pascal's Triangle in JavaScript by exploring two core techniques: recursion and functional composition. Understand how to define base and recursive cases, use function transformations, and apply function composition to generate each line efficiently.
We'll cover the following...
We'll cover the following...
Solution
The problem can be solved using either the function composition or recursion. Let’s look at the two approaches individually.
Recursion approach
For this solution, factor out the base cases and recursion cases.
The two cases can be outlined like this.
- Base case (lines 2 to 7): For zeroth line, the array will only have a single value of
1, so we can conclusively return[1]. - Recursive case (lines 8 to 24): For any other line, we have a value of
1at