0

I am trying to check if an object is within an array, I have managed to do this however I am unable to run the code within the blocks. Here is the code snippet:

basket.add = function(item) {
    for(var i = 0; i < this.items.length; i++){
        if(item === this.items[i]){
            basket.items[i].count += 1;
        }else{
            basket.items.push(item);  
        };
    };
    basket.print();
};

When I run the code without the if statement it works fine apart from the fact it push the obj again. When I check and then push the obj if it is not in the array this code does not work.

5
  • By "unable to run the code within the blocks." do you mean the count is not incrementing, or are you referring to one of the other code blocks? Commented Nov 25, 2014 at 20:23
  • Sorry, the count is working. Its the else that is not. Commented Nov 25, 2014 at 20:24
  • 1
    You shouldn't be using semi-colons to end if blocks and for blocks. Commented Nov 25, 2014 at 20:26
  • What data types are item and this.items, you are using === so it checks to see if variables are equal and the same type before returning result. We need more info Commented Nov 25, 2014 at 20:28
  • They are both existing objects created with the new constructor. Commented Nov 25, 2014 at 20:29

2 Answers 2

3

Your current code loops through items, pushing another reference to item to the end of the array each time it finds a value in items that doesn't match item. I don't think this is what you want.

Instead you can use

basket.add = function(item) {
    var i = basket.items.indexOf(item);
    if (i == -1) {
        basket.items.push(item);
    }
    else {
        basket.items[i].count += 1;
    }
};        

For users of IE8 and earlier .indexOf won't work, so if that's important you can add this polyfill.

Sign up to request clarification or add additional context in comments.

1 Comment

well done for for explaining the issue better than me!
1

You're looping over your items trying to determine if the item being added is already in the "basket":

for(var i = 0; i < this.items.length; i++){
    if(item === this.items[i]){
       ....

Im guessing you dont want to add the item once for each item in the array already! So move the adding of a new item outside the loop.

Another problem is that you are checking the length of this.items but then pushing to basket.items - this may be valid but I suspect not.

basket.add = function(item) {
    var foundItem = false;
    for(var i = 0; i < this.items.length; i++){
        if(item === this.items[i]){
            this.items[i].count += 1;
            foundItem = true;
            break;
        }
    };
    if(!foundItem){
         this.items.push(item);
    }
    basket.print();
};

2 Comments

This solved the problem. Thank you. Could you explain why it was not working?
I don't understand why it skipped over pushing it to the array. I will check it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.