Solved

Javascript for each certain amount, increase variable

Posted on 2013-06-17
7
222 Views
Last Modified: 2013-06-20
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
Comment
Question by:Letsgetcoding
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
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 ?
please confirm.

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>love to marry </title>
  <meta name="Generator" content="Lorem ipsum dolor sit amet,">
  <meta name="Author" content=" consectetur.adipisicing,">
  <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);
      alert(totalprice)
      alert(item)
      alert(extra*cost)


  }

  </script>
 </head>

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

(Edit: Possible advertising removed - Modulus_Twelve)
0
 

Author Comment

by:Letsgetcoding
Comment Utility
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 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
Comment Utility
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>

Open in new window

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);
  
});

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Letsgetcoding
Comment Utility
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 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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

by:Letsgetcoding
Comment Utility
Works like a charm. Just needed to put the maxshipping in where the /10 was.

Thank you!
0
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
Hi @Letsgetcoding
For 25 it has to 1.80 right?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now