Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a function that will display number according to specific range. ex from 1-29 show 1, from 30-59 show 2, from 60-89 show 3 and so on

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading