Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Using modular arithmetics for interval

$
0
0

Let's say we have range [-2, -1, 0, 1, 2, 3] which can be described as MIN_VALUE=-2 and MAX_VALUE=3 We want to implement function called infiniteCarousel() which will accept any number and used modulo operator (or somthing else) in order to calculate corresponding number in specified range.

function infiniteCarousel(value, minValue, maxValue)
{
    // todo
    // use modulus operator to calculate correct number
    // return calculated number 
}


// range = [-2, -1, 0, 1, 2, 3]
const MIN_VALUE = -2;
const MAX_VALUE = 3;

var array = [];

// let's calculate number from -10 to 10
for (var i = -10; i<10; i++)
{
    array.push( infiniteCarousel(i, MIN_VALUE, MAX_VALUE) );
}

// should print [2, 3, -2, -1, 0, 1, 2, 3, -2, -1, 0, 1, 2, 3, -2, -1, 0, 1, 2, 3, -2]
console.log(array);  

Do you know any efficient way to implement this? Also, does anybody know what is the correct name for this problem? I am having troubles searching web because I don't know how to properly describe this problem.


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>