

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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++
- Java program to print Pascal's triangle
- C# Program to create Pascal’s Triangle
- Finding area of triangle in JavaScript using Heron’s formula
- Program to generate Pascal's triangle in Python
- Java Program to Print Star Pascal's Triangle
- Finding the nth digit of natural numbers JavaScript
- Finding nth element of the Padovan sequence using JavaScript
- Finding the nth power of array element present at nth index using JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding sum of every nth element of array in JavaScript
- Finding nth digit of natural numbers sequence in JavaScript
Advertisements