
- 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
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 Questions & Answers
- Divide a string into n equal parts - JavaScript
- Count number of ways to divide a number in parts in C++
- Divide a number into two parts in C++ Program
- How to divide an unknown integer into a given number of even parts using JavaScript?
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Python - Ways to merge strings into list
- Python - Ways to convert array of strings to array of floats
- Split string into equal parts JavaScript
- Divide number into two parts divisible by given numbers in C++ Program
- Sorting parts of array separately in JavaScript
- Splitting a string into parts in JavaScript
- Divide Array Into Increasing Sequences in Python
- Splitting a string into maximum parts in JavaScript
- How to divide an array into half in java?
- Divide a big number into two parts that differ by k in C++ Program
Advertisements