- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Finding the longest common consecutive substring between two strings in JavaScript
- Longest possible string built from two strings in JavaScript
- Finding the longest consecutive appearance of a character in another string using JavaScript
- Finding longest consecutive joins in JavaScript
- Length of the longest possible consecutive sequence of numbers in JavaScript
- Finding the character with longest consecutive repetitions in a string and its length using JavaScript
- Program to find longest consecutive run of 1s in binary form of n in Python
- N consecutive odd numbers JavaScript
- Length of longest string chain in JavaScript
- Maximum sum of n consecutive elements of array in JavaScript
- Longest Consecutive Sequence in Python
- Maximum consecutive 1s after n swaps in JavaScript
- Finding all the longest strings from an array in JavaScript
- Removing consecutive duplicates from strings in an array using JavaScript
- Find the longest sub array of consecutive numbers with a while loop in JavaScript

Advertisements