0

I'm working on a Javascript exercise that requires a function that can search for an object with a certain property within an array. If such an object exists, the function should delete that object, then return the array. If no such object exists within the array, the function should return a string saying the object does not exist.

So far I have written the code below, taking the example of a shopping cart, but I'm not getting the desired result. If I search for the object at cart[0], it works as intended. However, anything with an index above 0 returns "That item is not in your cart." What am I doing wrong?

var cart = [{apples: 12}, {oranges: 20}, {grapes: 35}, {peaches: 18}]

function removeFromCart(item){ 

   for (var i = 0; i < cart.length; i++){     
    if (cart[i].hasOwnProperty(item)) {
      cart.splice(i, 1);
      return cart;
    }
    else {
      return "That item is not in your cart.";
    }
   }
  }
3
  • You may want to look into Array#filter. Commented Mar 13, 2017 at 6:07
  • @trincot - Sure it will, because it's not being called on cart. Commented Mar 13, 2017 at 6:10
  • Your loop is bad anyway, lets say item you want to remove is second in array, and when it starts going through for loop, on first item it will go to else, and stop the loop because of the return statement, it would never go through all of them. Commented Mar 13, 2017 at 6:12

2 Answers 2

1

The problem is that on the first iteration of your loop, if the if condition is not true it will do the else condition and return immediately. Get rid of the else and move the second return statement to after the loop - that way the "not found" condition is only applied after checking every item.

var cart = [{apples: 12}, {oranges: 20}, {grapes: 35}, {peaches: 18}]

function removeFromCart(item){ 
  for (var i = 0; i < cart.length; i++){     
    if (cart[i].hasOwnProperty(item)) {
      cart.splice(i, 1);
      return cart;
    }
  }
  // not found:
  return "That item is not in your cart.";
}

console.log(removeFromCart("grapes"));

As an aside, a function that returns an array sometimes and a string other times can be a little confusing to work with. Given that your function is updating a cart variable defined outside the function, I'd be more inclined to return an empty string for success and an error string for failure.

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

Comments

0

Hope this will be usefule

var cart = [{
  apples: 12
}, {
  oranges: 20
}, {
  grapes: 35
}, {
  peaches: 18
}]

function removeFromCart(item) {
  cart.forEach(function(elem, index) {
    if (elem.hasOwnProperty(item)) {
      cart.splice(index, 1);
    }
  })
  return cart; // return the array
}

console.log(removeFromCart('apples'))

DEMO

In my option it is better not to modify aan array inside a loop,infact you cna use filter function , without modifying the original array, which can resued

function removeFromCart(item) {

 var _x = cart.filter(function(elem){
  return !elem.hasOwnProperty(item)
  })
return _x
}

console.log(removeFromCart('apples'))

DEMO

Comments

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.