Solved

Javascript for each certain amount, increase variable

Posted on 2013-06-17
7
227 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
[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
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Insoftservice
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 ?
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
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 53

Accepted Solution

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

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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:Letsgetcoding
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 53

Expert Comment

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

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

Thank you!
0
 
LVL 15

Expert Comment

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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 add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

695 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