Search
 
SCRIPT & CODE EXAMPLE
 

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 ]
Comment

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 ]
Comment

PREVIOUS NEXT
Code Example
Javascript :: next day date javascript 
Javascript :: par ou impar js 
Javascript :: how to write a json in r 
Javascript :: reverse a string in javascript 
Javascript :: jquery grab table row 
Javascript :: how to login with api in react js 
Javascript :: dynamically added button onclick not working 
Javascript :: how avoid populate mongoose return password 
Javascript :: javascript for loop array 
Javascript :: get list of all attributes jqery 
Javascript :: js addeventlistener foreach 
Javascript :: momentjs 
Javascript :: require mongoose 
Javascript :: async await promise all javascript 
Javascript :: A bad HTTP response code (404) was received when fetching the script. 
Javascript :: node.js web server 
Javascript :: react router cannot read location of undefined 
Javascript :: react native password strength 
Javascript :: set localstorage value 
Javascript :: inline focus style 
Javascript :: queryselectorall in jquery 
Javascript :: watch file in changes in webpack 
Javascript :: how play audio js 
Javascript :: fatorial recursivo em javascript 
Javascript :: mutable array methods in javascript 
Javascript :: @paypal/react-paypal-js 
Javascript :: js get integer value of 
Javascript :: replace in javascript 
Javascript :: razor list to js array 
Javascript :: create new Next.js 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =