
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the elements of nth row of Pascal's triangle in JavaScript
Pascal's triangle:
Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows.
The first few elements of Pascals triangle are −
We are required to write a JavaScript function that takes in a positive number, say num as the only argument.
The function should return an array of all the elements that must be present in the pascal's triangle in the (num)th row.
For example −
If the input number is −
const num = 9;
Then the output should be −
const output = [1, 9, 36, 84, 126, 126, 84, 36, 9, 1];
Example
Following is the code −
const num = 9; const pascalRow = (num) => { const res = [] while (res.length <= num) { res.unshift(1); for(let i = 1; i < res.length - 1; i++) { res[i] += res[i + 1]; }; }; return res }; console.log(pascalRow(num));
Output
Following is the console output −
[ 1, 9, 36, 84, 126, 126, 84, 36, 9, 1 ]
- Related Articles
- Program to find the nth row of Pascal's Triangle in Python
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- Pascal's Triangle in C++
- Pascal's Triangle II in C++
- Finding the nth digit of natural numbers JavaScript
- Program to generate Pascal's triangle in Python
- Java program to print Pascal's triangle
- Finding nth element of the Padovan sequence using JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding the nth prime number in JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Finding nth digit of natural numbers sequence in JavaScript
- Java Program to Print Star Pascal's Triangle
- Finding nth element of an increasing sequence using JavaScript

Advertisements