
- 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 all the unique paths in JavaScript
Suppose we have an array of m * n order. A person starts at the start block of the 2-D array (0,0) and he wants to reach the end (m, n). The limitation is that at once, he can either move one step down or one step right.
We are required to write a JavaScript function that takes in the height and width of the 2-D grid.
The function should find out the number of unique paths that are available to the person to reach to the end.
Example
Following is the code −
const height = 3; const width = 4; const findUniquePath = (width = 1, height = 1) => { const board = Array(height).fill(null).map(() => { return Array(width).fill(0); }); for (let rowIndex = 0; rowIndex < height; rowIndex += 1) { for (let columnIndex = 0; columnIndex < width; columnIndex += 1) { if (rowIndex === 0 || columnIndex === 0) { board[rowIndex][columnIndex] = 1; } } } for (let rowIndex = 1; rowIndex < height; rowIndex += 1) { for (let columnIndex = 1; columnIndex < width; columnIndex += 1) { const uniquesFromTop = board[rowIndex - 1][columnIndex]; const uniquesFromLeft = board[rowIndex][columnIndex - 1]; board[rowIndex][columnIndex] = uniquesFromTop + uniquesFromLeft; } } return board[height - 1][width - 1]; }; console.log(findUniquePath(width, height));
Output
Following is the output on console −
10
- Related Articles
- Finding sum of all unique elements in JavaScript
- Unique Paths in Python
- Unique Paths II in C++
- Unique Paths III in C++
- Finding the sum of unique array values - JavaScript
- Finding unique string in an array in JavaScript
- Finding the only unique string in an array using JavaScript
- Finding first unique element in sorted array in JavaScript
- Finding the first unique element in a sorted array in JavaScript
- Finding all valid word squares in JavaScript
- Finding state after all collisions in JavaScript
- All-Pairs Shortest Paths
- Finding all the longest strings from an array in JavaScript
- Summing all the unique values of an array - JavaScript
- Finding all possible combinations from an array in JavaScript

Advertisements