Home > database >  How to create a function that will display number according to specific range. ex from 1-29 show 1,
How to create a function that will display number according to specific range. ex from 1-29 show 1,

Time:01-20

I need to create a function based on input value. Example: if we enter number from 1-29 then show 1, from 30-59 show 2, from 60-89 show 3 and so on...

I'm new to this and can't figure out how to make that. Please help...

CodePudding user response:

You can try something like this to make it more dynamic.

function getRange(num) {
    if (num <= 0) {
        return 'Invalid input';
    }
    let rangeSize = 30;
    let range = Math.floor(num/rangeSize) 1;
    return range;
}

Demo

function getRange(num) {
    if (num <= 0) {
        return 'Invalid input';
    }
    let rangeSize = 30;
    let range = Math.floor(num/rangeSize) 1;
    return range;
}

console.log(getRange(29))
console.log(getRange(30))
console.log(getRange(59))
console.log(getRange(62))

  • Related