- 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
All ways to divide array of strings into parts in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of literals of at least two elements.
Our function should return all the ways to divide the array into two non-empty parts.
For instance −
For the array ["az", "toto", "picaro", "zone", "kiwi"]
The possibilities are −
"(az, toto picaro zone kiwi)(az toto, picaro zone kiwi)(az toto picaro, zone kiwi)(az toto picaro zone, kiwi)"
Example
Following is the code −
const arr = ["az", "toto", "picaro", "zone", "kiwi"]; const findAllPossiblities = (arr = []) => { let array; const res = []; for(let i = 1; i < arr.length; i++){ array = []; array.push(arr.slice(0,i).join(" ")); array.push(arr.slice(i).join(" ")); res.push(array); }; return res; }; console.log(findAllPossiblities(arr));
Output
[ [ 'az', 'toto picaro zone kiwi' ], [ 'az toto', 'picaro zone kiwi' ], [ 'az toto picaro', 'zone kiwi' ], [ 'az toto picaro zone', 'kiwi' ] ]
- Related Articles
- Divide a string into n equal parts - JavaScript
- Count number of ways to divide a number in parts in C++
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- How to divide an unknown integer into a given number of even parts using JavaScript?
- Python - Ways to merge strings into list
- Python - Ways to convert array of strings to array of floats
- Divide a number into two parts in C++ Program
- Divide 29 into two parts so that the sum of the squares of the parts is 425.
- How to divide an array into half in java?
- Divide Array Into Increasing Sequences in Python
- Finding all the longest strings from an array in JavaScript
- JavaScript function to prepend string into all the values of array?
- Divide number into two parts divisible by given numbers in C++ Program
- How to get the numbers which can divide all values in an array - JavaScript
- Sorting parts of array separately in JavaScript

Advertisements