0

Ok so I writ some JQuery for updating a price live when a user adds an extra. There is one chunk of code for Booster Seats and one for Child Seats.

Here is one, so you can see exactly what I do:

    $('.boostseat').change(function() {
        //sets the var id as the id of the select box
        var id = this.id;
        //sets this as the value of the select box
        numberofboostseat = $(this).val();
        //shows a confirmation message
        $('#boostseatadd-'+id).show();
        //this handles if an item is removed, so that the value doesn't reset
        if (typeof(boosttotal) != "undefined" && boosttotal !== null) {
            price = $("#hiddenprice").val() - boosttotal;
        }else{
            price = $("#hiddenprice").val();
        }
        //number of days you hire it for cariable
        numofdays = "<?php echo $length->days; ?>";
        if (numofdays > 4){
            //alert ("boost if1");
            boostcost = Number(20);

        }else if ((numberofboostseat == 1 || numberofboostseat == 0) && (numberofchildseat == "undefined" || numberofchildseat == 0)){

            //alert ("boost if2");
            boostcost = Number(3) * Number(0);

        }else if (numofdays < 1){
            //alert ("boost if3");
            boostcost = Number(3) * Number(1);

        }else{
            //alert ("boost if4");
            boostcost = Number(3) * Number(numofdays);
        }


        // determines final price
        boosttotal = Number(numberofboostseat) * Number(boostcost);
        //more varaibles 
        newprice = Number(price) + Number(boosttotal);
        //updates hiddden fields
        $("input#hiddenboostprice").val(boosttotal);
        $("input#hiddenboostnum").val(numberofboostseat);
        $("input#hiddenprice").val(newprice);

        oldnumofboost = $("input#hiddenboostnum").val(numberofboostseat);
        //updates the HTML (Live Price Update) as decimal
        $('#'+id).html("&euro;" + newprice.toFixed(2));


    });

});

I've tried to comment it a bit to make it clearer. There is another chunk of code like this that handles child seats.

My problem is the the first childseat OR booster seat is free.

I tried this:

}else if ((numberofboostseat == 1 || numberofboostseat == 0) && (numberofchildseat == "undefined" || numberofchildseat == 0)){

As you can see above, the variable boostcost counts the cost of the seats. The other chunk of code, the childseat one also has global variables i can use, similar to these.

Can anyone suggest an if statement that would effectively make the first child OR booster seat free?

6
  • Why do you use Number(x) ? Commented Mar 11, 2013 at 21:41
  • I just thought it was a safe way to make sure the variables were treated as numbers? I'm no JQuery expert though.... Commented Mar 11, 2013 at 21:43
  • What does that have to do with jQuery? Anyway, you used it with number literals, not variables. Commented Mar 11, 2013 at 21:44
  • Lets say a users buys 3 booster seats, and 5 child seats. Is 1 booster seat free? Or is 1 child seat free? Commented Mar 11, 2013 at 21:51
  • 1 of either. If they have 1 child seat and 1 booster seat. only the first one they selected is free. If that makes sense. The rule is the first childseat OR booster seat is free. Commented Mar 11, 2013 at 21:52

1 Answer 1

1

I am adding .discount = true to variables after they have been decremented to prevent the sister varialbe from being decremented as well. This way numberofchildseat or numberofboostseat will be discounted by 1 but never both. As long as .discounted has not been set javascript will evaluate it as false.

   if(!numberofchildseat.discounted && numberofboostseat > 0){
        numberofboostseat--;
        numberofboostseat.discounted = true;
   }
   if(!numberofboostseat.discounted && numberofchildseat > 0){
        numberofchildseat--;
        numberofchildseat.discounded = true;
   }
Sign up to request clarification or add additional context in comments.

4 Comments

I'm really sorry if this is a stupid question, but this line numberofchildseat--; does what exactly?
Its the opposite of foo++. Its the same as numberofchildseat = numberofchildseat - 1;
I'm sorry I got pulled away from the computer, I will be implementing it this morning and will let you know.
I have tried this, although I don't think I am understanding correctly, When you say you are adding .discount = true after the variable is used?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.