4ndy
4ndy

Reputation: 448

Price Calculations (Based on user input amount levels)

I am basing this code off of this jsfiddle:

http://jsfiddle.net/vxupE/

I would love to implement these price levels into my code so that when a person puts in $501 they are charged 20% but if they put in $500 they are charged 25%

if $price =< 101 then *.30
if $price > 100 and < 501 then *.25
if $price > 500 and < 1501 then *.20
if $price > 1500 then *.15

I'm at a loss when is comes to javascript. I think this could help others though. Would definitely help me! :)

Upvotes: 0

Views: 293

Answers (2)

otherDewi
otherDewi

Reputation: 1098

There are two ways I would consider.

switch statements, there are plenty of tutorials on line to look through. The other option is is using if and else if To find the correct additional charge.

if($price =< 101){
   charge=0.3;
}else if($price>=100){
   charge=0.25;
}else if($price>=500){
   charge=0.2;
}else if($price>=1500){
   charge=0.15;

where charge can then be used to calculate the final cost.

UPDATE

your code is checking if your sub total value is equal to your zip code. change

$('#sub_tot').change(function(){ to $('#zip').change(function(){ to

Upvotes: 0

WannaCSharp
WannaCSharp

Reputation: 1898

Try this:

$('#sub_tot').change(function(){
    var value = $(this).val();
    var charge = 0;
    if ( value >= 501 ) {
     charge = .25;   
    }
    else if ( value >= 100 ) {
     charge = .20;  
    }
    $('#tax').val( (value * charge).toFixed(2) );
}).change()

Upvotes: 1

Related Questions