Can someone help me with this kind of system

Hi, i need your help. I’m trying to do something like this

[CODE]

function func()
{
var input = document.getElementById(‘num’).value;
var output = document.getElementById(‘para’);

        var a = 60;
        
        if(input <= 10) {
            inputx = a;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 10 && input <= 20) {
            inputx = a * 2;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 20 && input <= 30) {
            inputx = a * 3;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 30 && input <= 40) {
            inputx = a * 4;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 40 && input <= 50) {
            inputx = a * 5;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 50 && input <= 60) {
            inputx = a * 6;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 60 && input <= 70) {
            inputx = a * 7;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 70 && input <= 80) {
            inputx = a * 8;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 80 && input <= 90) {
            inputx = a * 9;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
        
        else if(input > 90 && input <= 100) {
            inputx = a * 10;
            output.innerHTML = "Seems like you have " + input + " POINTS, so you get: " + inputx + " EXP.
        }
</script>
Calculate

[/CODE]

Let me explain. When a user enter some number in the input field and press the calculate button, according some system like this

He get an answer on that output.
If he enter 100, the output should be 600 (60 * 10 because it’s between 90 - 100, >90 but <= 100 )
if he enter 150, the output should be 900 (60 * 15)
if he enter 109, the output should be 660 (60 * 11 because it’s between 100 - 110)
if he enter 111, the output should be 720 (60 * 12 because it’s between 110-120)
My code it is working but isn’t best pratice. I must write thousands of lines for each case if user enters a big value. So, i need help doing this in a few lines. Sorry my incredible bad English, i’m such a noob.

Hi @gamer_andrei, you can use Math.floor() and a regular division to get the number in a given range; like

function calculate (value) {
  return Math.floor(value / 10) * 60
}

Thanks a lot man. I did it, thanks a lot again.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.