Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

print all the subarrays of an array

let array = [1, 2, 3, 4, 5]

for (let i = 0; i <= array.length; i++) {
    if (i === 0)
        console.log([])   // empty array is also subarray

    for (let left = 0, right = i; left < array.length, right < array.length; left++, right++) {
        console.log(array.slice(left, right + 1))
    }
}

// [Log]
// []
// [ 1 ]
// [ 2 ]
// [ 3 ]
// [ 4 ]
// [ 5 ]
// [ 1, 2 ]
// [ 2, 3 ]
// [ 3, 4 ]
// [ 4, 5 ]
// [ 1, 2, 3 ]
// [ 2, 3, 4 ]
// [ 3, 4, 5 ]
// [ 1, 2, 3, 4 ]
// [ 2, 3, 4, 5 ]
// [ 1, 2, 3, 4, 5 ]
 
PREVIOUS NEXT
Tagged: #print #subarrays #array
ADD COMMENT
Topic
Name
4+9 =