Click here to Skip to main content
14,429,492 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to give 10% discount for the total price above 200 in shopping cart. I had given following code but it is not working. Pl. give me proper code.

What I have tried:

     getDiscountPrice: function(products, totalPrice, totalQuantity) {
       var x = 200
     if (totalPrice > x) {
       DiscountPrice = totalPrice - (totalPrice * .10)
     return totalPrice > x;

       console.log("calculating discount" totalPrice > x);

Updated 4-Nov-19 2:03am
F-ES Sitecore 4-Nov-19 7:21am
You need to actually take time to learn and understand javascript. If you're just asking for code on forums and pasting it into your coursework without understanding it then you're not learning anything.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

return totalPrice > x;

That returns a boolean value of true or false, and the calculated DiscountPrice value is thrown away after it is calculated. Also, why are you passing in products and totalQuantity since you do not use either value anywhere in your calculation.

Think about the simple steps needed to do this:
the function only needs to know the current total price, and the steps are something like:
1. set newprice = totalprice.
2. if totalprice > x then set newprice = totalprice * .90
3. return newprice // if totalprice is less than x, then original total will be returned

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100