mercredi 4 janvier 2017

loop instead of if statements for calculation

I’m trying to figure out how I can get this code shorten into a loop or so.

Basically I have two input fields that need to be filled:

<input type="number" id="people" name="people" value="0"> 
<input type="text"  id="upprice" name="upprice" value="0">

This is the current situation how I solved it:

var upPrice = 0,
    people  = 0;

$('#people').on('change', function() {
    countValue = this.value;

    if (countValue >= 10) {
        upPrice = 1000;
    } 

    if (countValue >= 20) {
        upPrice = 2000;
    } 

    if (countValue >= 30) {
        upPrice = 2500;
    } 

    if (countValue >= 40) {
        upPrice = 3000;
    } 

    $('#upprice').attr('value', upPrice);
});

Technically it’s a calculation that fires an upcharge every 10th step. First to steps the upcharge has a value of 1000, the next two 500 and so on…and it goes up to 250.

I guess the most rational method to do this would be a loop, right? Unfortunately I’m not able to get the loop right, can anyone help me out or gimme an advice how to solve this properly?

Aucun commentaire:

Enregistrer un commentaire