mercredi 3 février 2016

Looping through results and changing value depending on distance

Im trying to loop through an array of objects. I want to change the rating depending on the distance away. For example if the distance is 10-20 miles away the rating will drop.

This is what i have done but I'm unsure why its not working because I'm not getting any errors.

<script>

    var products = [{product:"Shoe", Rating:"4", Distance:"2"},{product:"Trousers", Rating:"5", Distance:"5"},{product:"Glasses", Rating:"3", Distance:"7"},{product:"Hat", Rating:"2", Distance:"10"}];
    var rating = products[1];
    var distance = products[2];

    for (var i=0; i < products.length; i++) {
        if (distance >= 0 && distance <=4)
            rating = rating+1;
        else if (distance >= 5 && distance <=10)
            rating = rating-1;
        else if (distance >= 11 && distance <=20)
            rating = rating-2;
        else if (distance >= 21 && distance <=100)
            rating = rating-3;
        else if (distance >= 101 && distance <=10000)
            rating = rating-4;
        else
            rating = rating;
        console.log(products[i]);
    }

    </script>

Aucun commentaire:

Enregistrer un commentaire