How to generate a random multiple of a given number bounded to a given range?

  javascript, random

How can I generate a random number which is a multiple of a given number while also making sure the number is also within a specified range?

const generateMultiple = (multipleOf, min, max) => {
    //how to implement?
}

generateMultiple(4, -8, 5)
//should return either -8, -4, 0, 4

generateMultiple(2.1, 1, 5)
//should return either 2.1, 4.2

generateMultiple(7, 1, 5)
//should return NaN

Source: Ask Javascript Questions

LEAVE A COMMENT