- 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

# Longest string consisting of n consecutive strings in JavaScript

## Problem

We are required to write a JavaScript function that takes in an array of strings. Our function should create combinations by combining all possible n consecutive strings in the array and return the longest such string that comes first.

## Example

Following is the code −

const arr = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; const num = 2; function longestConsec(strarr, k) { if (strarr.length == 0 || k > strarr.length || k <= 0) return ''; let longStr = ''; let newStr = ''; for (let i = 0; i < strarr.length; i++){ newStr = strarr.slice(i, i+k); if (newStr.join('').length > longStr.length ){ longStr = newStr.join(''); } } return longStr; } console.log(longestConsec(arr, num));

## Output

abigailtheta

- Related Questions & Answers
- Finding the longest common consecutive substring between two strings in JavaScript
- Finding longest consecutive joins in JavaScript
- Longest possible string built from two strings in JavaScript
- Finding the longest consecutive appearance of a character in another string using JavaScript
- Length of the longest possible consecutive sequence of numbers in JavaScript
- N consecutive odd numbers JavaScript
- Longest Consecutive Sequence in Python
- Program to find longest consecutive run of 1s in binary form of n in Python
- Length of longest string chain in JavaScript
- Maximum sum of n consecutive elements of array in JavaScript
- Finding the character with longest consecutive repetitions in a string and its length using JavaScript
- Longest Line of Consecutive One in Matrix in C++
- Maximum consecutive 1s after n swaps in JavaScript
- Binary Tree Longest Consecutive Sequence in C++
- Find the longest sub array of consecutive numbers with a while loop in JavaScript

Advertisements