简体   繁体   中英

round nearest 200 but -1 if 200

I have a queue of songs but starts displaying from 0 - 200. When you go next page, it starts from 201 - 400. Next page, 401 to 600 etc.

I have an auto scroll function but i need it to jump to the page the current song is playing on then scroll to the song? So if the song is: 250, i no the start point should be 201 and end display at 400. But if the song is 200, i no the start is 0 and end is 200. I need to come up with a solution to round to the nearest 200 is its 250 260 etc but if its 200,400,600 etc then -1 so if its 200, -1 is 199 and it will round it to 0. Leaving things not AS complicated!

Perhaps there is a better solution? Below is my code anyway for rounding <- which works nice!

Code:

scrolltopage = destination + 1; //number could be 20,201,288 etc 
startpageQueueScroll = parseInt(Math.floor(scrolltopage / 200) * 200 + 1); //Start displaying contents at
endpageQueueScroll = parseInt(Math.floor(scrolltopage / 200) * 200 + 201); //Stop displaying at 
console.log("Start: " + startpageQueueScroll + " and end page: " + endpageQueueScroll); //Display Output

How about using the modulo operator? Sample code:

    function getNextPages(original){
    const nextDestination = original + 1;
    const nextModulus = nextDestination % 200;
    if(nextModulus == 0){
        return -1;
    }
    const nextMinPage = nextDestination - nextModulus + 1;
    const nextMaxPage = nextMinPage + 200;
    console.log(nextMinPage);
    console.log(nextMaxPage);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM