Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Javascript for each certain amount, increase variable

Posted on 2013-06-17
Medium Priority
231 Views
Hi there,

I am needing to calculate shipping in a website I am writing. Each item we sell has a maxshipping variable which shows how many of that item can be shipped before the items shipping cost is increased.

E.g.
An item has a shipping cost of \$0.60 for up to 10 of the items... After that 10 I want to increase it by another \$0.60... then so forth for each 10 items.

How do I go about doing this?

Thanks,
Jessee
0
Question by:Letsgetcoding
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2

LVL 15

Expert Comment

ID: 39255376
so you mean to say for 10 items its \$0.60
now if it have only one item it would be also \$0.60 right ?

If yes please copy below code and save it as "test.html".

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<title>love to marry </title>
<meta name="Generator" content="Lorem ipsum dolor sit amet,">
<meta name="Keywords" content="sed do eiusmod tempor incididunt">
<meta name="Description" content="ut labore et dolore magna aliqua.">
<script type="text/javascript">

function bridedress()
{
var item_no =25;
var cost = 0.60;
var quantity =10;
var extra = 0;
var item = 0;
var totalprice =0 ;
var extraitem =0 ;

extraitem = (item_no%quantity);
item = Math.floor(item_no/quantity);
if(extraitem < quantity)
{
extra = 1;// to keep additional item less than 10 as 1 unit or quantity
}
totalprice = (item*cost)+(extra*cost);

}

</script>

<body>
<a href="javascript:bridedress()">lovetomarry</a>
</body>
</html>

(Edit: Possible advertising removed - Modulus_Twelve)
0

Author Comment

ID: 39255432
Hi there,

Yes.
1 - 10 items = 0.60
11 - 20 items = 1.20 etc etc...

Will try this code and get back to you.

Thanks,
Jessee
0

LVL 54

Accepted Solution

Scott Fell,  EE MVE earned 2000 total points
ID: 39255459
Here is a working sample I made up  using jquery http://jsbin.com/ogilub/1/edit

You can take this as a base.  In short, you get the qty and divide by 10 and round up using Math.Ceil.  This way a qty of 11  gets multiplied by 2 *.60
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil

HTML
``````<lable><input id="qty" value="0">Qty</lable>
<lable><input id="shipping" value="0">Shipping</lable>
``````
JS
``````var qty=0;
var ship_cost=0;

\$('#qty').change(function(){
qty=\$('#qty').val();
ship_cost= (Math.ceil((qty/10)))*0.6;
//console.log(ship_cost);
\$('#shipping').val(ship_cost);

});
``````
0

Author Comment

ID: 39258424
Hey there,

The maxshipping changes with each item. Sometimes only 2 items can be sent for 0.60... other times 10 items can be. How do I include that?

Thanks,
Jessee
0

LVL 54

Expert Comment

ID: 39258451
Did you try it?  If you add 1 through 10 in the qty box, it will be 60 cents. If you use 11 through 20 it will be 1.20.
0

Author Closing Comment

ID: 39258559
Works like a charm. Just needed to put the maxshipping in where the /10 was.

Thank you!
0

LVL 15

Expert Comment

ID: 39261697
Hi @Letsgetcoding
For 25 it has to 1.80 right?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
###### Suggested Courses
Course of the Month11 days, left to enroll