Object help needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MikeS

    Object help needed

    I'm trying to add items to a shopping cart, if the item is already added I
    want to just increase the quantity by 1 instead for adding a new Item, I've
    got it nearly working for the first item ...but after that it just keeps
    adding a new Item. What am I doing wrong - I've been racking my brains
    trying to figure this out all day.

    function additem(desc,pr ice,quantity) {
    if (numitems>=1){
    for(i=1;i<=numi tems;i++) {
    if(cart[i].desc == desc){
    ++cart[i].quantity
    }
    else{
    cart[++numitems] = new Item(desc,price ,quantity);
    }
    displaycart();
    }
    }else{
    cart[++numitems] = new Item(desc,price ,quantity);
    displaycart();
    }
    }

    Thanks
    Mike


  • Janwillem Borleffs

    #2
    Re: Object help needed


    "MikeS" <nospam@secure. f9.co.uk> schreef in bericht
    news:behmdh$5c3 dc$1@ID-27598.news.dfnc is.de...[color=blue]
    > I'm trying to add items to a shopping cart, if the item is already added I
    > want to just increase the quantity by 1 instead for adding a new Item,[/color]
    I've[color=blue]
    > got it nearly working for the first item ...but after that it just keeps
    > adding a new Item. What am I doing wrong - I've been racking my brains
    > trying to figure this out all day.
    >
    > function additem(desc,pr ice,quantity) {
    > if (numitems>=1){
    > for(i=1;i<=numi tems;i++) {
    > if(cart[i].desc == desc){
    > ++cart[i].quantity
    > }
    > else{
    > cart[++numitems] = new Item(desc,price ,quantity);
    > }
    > displaycart();
    > }
    > }else{
    > cart[++numitems] = new Item(desc,price ,quantity);
    > displaycart();
    > }
    > }
    >[/color]

    When the second condition equals true, the value of numitems will be
    increased, which causes an infinite loop.

    Instead of using [++numitems], do it like [numitems+1]. This doesn't affect
    the value of numitems.


    JW



    Comment

    • Daniel

      #3
      Re: Object help needed


      "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
      news:3f0c870f$0 $28903$1b62eedf @news.euronet.n l...[color=blue]
      > When the second condition equals true, the value of numitems will be
      > increased, which causes an infinite loop.
      >
      > Instead of using [++numitems], do it like [numitems+1]. This doesn't[/color]
      affect[color=blue]
      > the value of numitems.[/color]

      Just adding to this:

      The ++ operand always increases a variable's value by 1, but works in two
      ways depending on where you put it -

      var i = 0;
      var j = 0;

      alert(i++); // Outputs 0
      alert(i); // Outputs 1

      alert(++j); // Outputs 1
      alert(j); // Outputs 1

      - in other words, putting ++ *before* the variable increases its value
      *before* it's evaluated, putting ++ *after* increases the value *after*. So,
      these for loops do exactly the same:

      for(var i=0;i<myArray.l ength; i++){
      alert(myArray[i]);
      }

      for(var i=0;i<myArray.l ength;){
      alert(myArray[i++]);
      }

      for(var i=-1;i<myArray.len gth-1;){
      alert(myArray[++i]);
      }

      Just in case you didn't know =)


      Cheers,
      Daniel



      --
      There are 10 kinds of people: Those who know binary and those who don't.


      Comment

      • MikeS

        #4
        Re: Object help needed

        "MikeS" <nospam@secure. f9.co.uk> wrote in message
        news:behmdh$5c3 dc$1@ID-27598.news.dfnc is.de...

        I'm nearly there...

        Ok, after looping through the array and only if the item is not found I want
        to add this as a new Item What am I missing from the code below?

        function additem(desc,pr ice,quantity) {

        for(i=1;i<=numi tems;i++) {
        if(items[i].desc == desc){
        ++items[i].quantity;
        }
        }

        items[++numitems] = new Item(desc,price ,quantity);

        displaycart();
        }



        Comment

        Working...