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

106
Views
how print all subarray of giving array (short type soltuion on javascript

I wanna solutions of this problem plc help

var s = [1, 2, 3, 4, 5, 6];
for (var i = 0; i < s.length; i++) {
  for (var j = i; j < s.length; j++) {
    str = "";
    for (var k = i; k <= j; k++) {
      str += s[k];
    }

    console.log(str);
  }
}

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

0

Did you want contiguous subarrays? Because that's what it seems from your code. Also you are not returning an array of subarrays, you are returning an array of strings made from subarrays.

Here I modified your code to output all the contiguous subarrays of an array:

function printSubArray() {
  var s = [1, 2, 3, 4, 5, 6];
  let res = [[]]
  for (let len = 1 ; len <= s.length; len++) {
    for (var j = 0; j < s.length - len + 1; j++) {
      let temp = []
      for (var k = j; k < j + len; k++) {
        temp.push(s[k])
      }
      res.push(temp)
    }
  }
  return res
}

console.log(printSubArray())
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!