Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
How to write a javascript program that returns the subset of a string as an array?

I have seen examples but I still dont understand all of it. I know substring() is definately used but I need a step by step breakdown if possible. Thank you very much :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First you can split the string using String.split() method and then you can create combinations with the elements by pushing in new array.

Working Demo :

// Input string
let str = "dog";
// An empty array which will store the combinations.
let result = [];

/**
 * getCombination() method is used to get all the combinations of all the array elements passed as a parameter.
 */
function getCombination(inputArr) {
  // variable which will store the combination string.
  let temp = '';
  // Iterating input array to get elements one by one.
  inputArr.forEach((elem, index) => {
    // Pushing the element into result array.
    result.push(temp + elem);
    // To make the combination updating temp variable with the elem. 
    temp += elem + "";
  });
}
 
// Once combination done for one iteration, removing the first element from an array and then again calling getCombination() method to get the combination for next set of elements.
str.split('').forEach((elem, index) => {
    getCombination([...str.slice(index)]);
});

// Expected result
console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!