

- 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
Dynamic programming to check dynamic behavior of an array in JavaScript
We are required to write a JavaScript function that takes in an array of strings, ordered by ascending length.
The function should return true if, for each pair of consecutive strings, the second string can be formed from the first by adding a single letter either at the beginning or end.
For example: If the array is given by −
const arr = ["c", "ca", "can", "acan", "acane", "dacane"];
Then our function should return true
Therefore, let’s write the code for this function.
Example
The code for this will be −
const arr = ["c", "ca", "can", "acan", "acane", "dacane"]; const isProgressive = arr => { for(let i = 0; i < arr.length-1; i++){ const nextLength = arr[i+1].length; if(arr[i+1] === arr[i+1][0] + arr[i] || arr[i+1] === arr[i] + arr[i+1][nextLength-1] ){ continue; }; return false; }; return true; }; console.log(isProgressive(arr));
Output
The output in the console will be −
true
- Related Questions & Answers
- Dynamic Programming in JavaScript
- Introduction to Dynamic Programming
- Dynamic Programming - Part sum of elements JavaScript
- Formatting dynamic json array JavaScript
- Dynamic Programming: Is second string subsequence of first JavaScript
- Dynamic Programming: return all matched data in JavaScript
- In JavaScript, need to perform sum of dynamic array
- Bitmasking and Dynamic Programming in C++
- Implementation of Dynamic Array in Python
- Dynamic imports in JavaScript.
- Sum over Subsets - Dynamic Programming in C++
- Difference Between Greedy Method and Dynamic Programming
- C++ Program to Find Fibonacci Numbers using Dynamic Programming
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- C++ Program to Perform Optimal Paranthesization Using Dynamic Programming
Advertisements