I have an array which consist n element.
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
and also have two button next and prev and initially i am showing the first 5 element(on page load) of an array initial array [1, 2, 3, 4, 5]
how i can show the next five elements on the next button click [6, 7, 8, 9, 10] and on previous button click want to show the [1, 2, 3, 4, 5]
and also need to check the if it does not have any next element if lastIndex is included and if array includes first element.
i have tried using slice to arr.slice(begin[, end])
const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
const size = 5
let current = 1
function prev(){
if(current > 1){
current--
let newArr = getNewArr()
console.log(newArr)
}
}
function next(){
if(current >= 1 && current <= (arr.length - 1)/size){
current++
let newArr = getNewArr()
console.log(newArr)
}
}
function getNewArr(){
return arr.slice(size*current - size ,size*current)
}
<button onclick=prev()>prev</button>
<button onclick=next()>next</button>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments