# How can i choose 5 elements from array?

for ex : let testArray = [1,2,3,4,5,6,7,8,9,10]
how can i take only items from 1 to 5?

is that depends, is it value or index specific?

lets say you have:

``````[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
``````

would you then take 10 till 6?

Javascript has built-in methods for this, or you could write your own little program/algorithm. Depending on requirements and what you are after.

1 Like

Thank you very much, Iâm working on To-Do List app project and i want to implement pagination after every 5 element,I managed to set max length of the items which get display=ânoneâ after it hits maximum length,but the problem is then i m clicking on the page button it displays all of the elements which was written
And i have no idea on how to do it

that makes things more complicated, so much better if you included those details from the beginning.

well, you would need a couple of things, you would need:

currentPage
elementsPerPage
lastPage

lastPage can simple be a boolean.

if you know the current page, and the elements per page you could use that for your slice. Given you can calculate the start of the slice (`currentPage * elementsPerPage`) and the stop of the slice (`(1 + currentPage) * elementsPerPage`)

1 Like

If you had a function that you call with the page number and the array, which returned a new array with the corresponding page (an array of 5 values), would that solve the problem?

And if you were to make such a function, then youâd start by creating an empty array, right? What then? Where would you start? Where does the first page start? Where does the second page start? The third? Is there a pattern? After deciding where to start, what then, youâd take five elements, thatâs repetition, a loop.

You could also look at what methods an array has. Maybe thereâs one that already does this.

1 Like

Thank you all for the answers,Right now i tried to do slice like this:const elementControl = () => {
const elementsPerPage = 5;
let emptyArr =
if (currentPage >=1) {
const sliceStart = (currentPage * elementsPerPage)
const sliceStop = ((1 + currentPage) * elementsPerPage)
let newArr = items.slice(sliceStart,sliceStop)
emptyArr.push(newArr)

}
}

but it returns empty arrays,Can you help me please? (Also Iâm very sorry Iâm new to javascript and Iâm not good in some of it parts)

incompleteTaskHolder is div which hold all entered LIs

Rather than stopping at that âthe whole functionâs result isnât rightâ you could look at the individual operations in the function. Thereâs a bunch of actions to carry out. Are they successfully being carried out? Print out the involved values and make observations.

1 Like

You might first want to look at whether your function has access to all the information it needs to carry out its work. The input.

Then you might want to look at what the final outcome is. Given those inputs, is it the output wrong?

And if youâve determined that there is indeed a problem in the function, then you can continue looking at the steps carried out. Again, youâd check the input and output of each operation that youâre interested in.

Narrow down as much as you can, and while you do, make sure that you are really seeing incorrect behaviour in the region that you are narrowing it down to so that you know youâre on the right track.

At some point youâll have narrowed it down enough that itâll be some individual operation being wrong, or maybe it should have been a different operation, or maybe the whole plan makes no sense. But all of those are useful information for how to continue whether thatâs looking up documentation for some function or picking a different operation or reconsidering what actions should be carried out there. Maybe youâd find that thereâs input missing, which would make it an impossible task until that has been supplied.

1 Like