Checking progressive array - 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.

How It Works

The algorithm checks each consecutive pair of strings to verify that the longer string can be formed by adding exactly one character to either the beginning or end of the shorter string.

Example

Following is the code ?

const arr = ["c", "ca", "can", "acan", "acane", "dacane"];

const isProgressive = arr => {
    for(let i = 0; i 

Output

Following is the output in the console ?

true

Step-by-Step Analysis

Let's trace through the example array to understand how the function works:

const arr = ["c", "ca", "can", "acan", "acane", "dacane"];

// Check each transition:
console.log("c" + "a" === "ca");        // true (add 'a' at end)
console.log("a" + "can" === "acan");    // true (add 'a' at beginning) 
console.log("acan" + "e" === "acane");  // true (add 'e' at end)
console.log("d" + "acane" === "dacane"); // true (add 'd' at beginning)
true
true
true
true

Alternative Implementation

Here's a cleaner version that's easier to understand:

const isProgressiveArray = (arr) => {
    for(let i = 0; i 

true
true
false

Conclusion

A progressive array requires each string to be formed by adding exactly one character to the previous string. The function efficiently validates this by checking both possible positions (beginning or end) for each consecutive pair.

Updated on: 2026-03-15T23:18:59+05:30

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements