Emil
Emil

Reputation: 55

Count how many times a functions is called, with different parameters

I have a function that receives an int as a parameter:

function addToCart(id){
    var cart= cart.info;
    for( i = 0; i < cart.length; i++){
        // SOME CODE
    }
}

The ID, that the addToCart function is receiving, is a number that other stuff on the shop got. For example, it will receive 3 if the user select movie, or 2 if the user selects computers etc.

All of that works, but what I want is it to count how many times a user have called on the movies for example, or computers. So when they view the shopping cart it will stand "You have selected movies X times". Is there an easy way to do so?

EDIT #1

var test1= {
    test: [
        {
            name: "Test1",
            image: "..",
            info: "Test1",
            price: 159.50
        }, {
            name: "Test2",
            image: "..",
            info: "Test2",
            price: 159.50
        }, {
            name: "Test3",
            image: "..",
            info: "Test3",
            price: 159.50
        }, {
            name: "Test4",
            image: "..",
            info: "Test4",
            price: 159.50
        } 
    ]   
}

For-loop:

for( i = 0; i < test1.length; i++){
    if(id == [i]){
        if($("#testDiv" + [i]).length == 0) {
            $("#stuff").append('<div id="test1'+ [i] +'"></div>');
            $("#test1" + [i]).append('//WRITE OUT INFO ABOUT JSON-object');
            $("#test1" + [i]).append('//WRITE OUT INFO ABOUT JSON-object');
            $("#test1" + [i]).append('//WRITE OUT INFO ABOUT JSON-object');
        } 
    }
}

The for-loop is inside of the addToCart, and the Jquery-stuff inside the forloop runs if an user add things to the cart. But, only if the div ID not exist. If it exist then I need to count how many times the item have been clicked.

With this method I get it to work:

if([i]  == 0){
                    test1Sum++;
                    $("#total" + [i] ).html(test1Sum);
                } else if([i]  == 1) {
                    test2Sum++;
                    $("#total" + [i] ).html(test2Sum);
                } else if ([i]  == 2){
                    test3Sum++;
                    $("#total" + [i] ).html(test3Sum);
                } else if ([i] == 3){
                    test4Sum++;
                    $("#total" + [i] ).html(test4Sum);
                }

But is there any better way to do so? Because for each new object I add to the JSon list I'll have to write a new else if - statement

Upvotes: 0

Views: 554

Answers (6)

Emil
Emil

Reputation: 55

This did the trick for me:

var TotalCheck= {}
    // Checks if a item exist
        if($("#totals" + [i]).length !== 0){
            TotalCheck["something" + [i]]++;
            $("#totals" + [i]).html(TotalCheck["something" + [i]]);
        }

And then I have this in my shopping cart so get how many how items that I have in cart

$("#totals" + [i]).html(TotalCheck["something" + [i]] = 1);

Upvotes: 0

Cameron Tinker
Cameron Tinker

Reputation: 9789

You need to keep a count of each type of item in the shopping cart. If you have a single page for viewing shopping cart contents, something like this could work:

var itemCount = {};

function addToCart(id){
    var productName = getName(id); // you will need to implement this

    if(itemCount[productName] != null)
        itemCount[productName]++;
    else
        itemCount[productName] = 0; // initialize

    alert('You have ' + itemCount[productName] + ' ' + productName + '(s) in your cart');
}

function removeFromCart(id) {
    var productName = getName(id); // you will need to implement this

    if(itemCount[productName] != null)
        if(itemCount[productName] > 0)
            itemCount[productName]--;
    else
        itemCount[productName] = 0;

    alert('You have ' + itemCount[productName] + ' ' + productName + '(s) in your cart');
}

You'll need to implement the product name lookup, but by using the bracket notation for object properties, you can dynamically initialize and update product counts in your cart.

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

you can make an array:

var items = new Array();

function addToCart(id){
     items.push(id)


}

and on item removing from Cart, remove it:

 items.pop(id);

Get the total number of selected movies like this:

var total = item.length;

Upvotes: 0

Jordan.J.D
Jordan.J.D

Reputation: 8113

var counters = { movies: 0, computers: 0 };

if(id === 3){
   counters.movies +=1;
}

Edit the above to include all of the cases and display it to your user after. This will keep separate counts for each of your IDs.

Upvotes: 1

mplungjan
mplungjan

Reputation: 178285

Sure

var ids = ["","","computers","movies"];
cart.counter = {};
function addToCart(id) {
  if (cart.counter[ids[id]]!=null) cart.counter[ids[id]]++;
  else cart.counter[ids[id]]=0;
  var msg ="";
  for (var tp in cart.counter) {
    msg+="\nYou called "+ tp + ":"+cart.counter[tp])
  }
  alert(msg);
}

Upvotes: 1

Jack_of_All_Trades
Jack_of_All_Trades

Reputation: 11468

Create one global function counter or counter specific to your scope and use caller method.

var func_counter=0;

function addToCart() {


   if (addToCart.caller == null) {
      return ("The function was called from the top!");
   } else
      func_counter++;
      return WHATEVER_YOU_WANT_TO;
}

Upvotes: 0

Related Questions