0

I would like my function to create an object and then push it to an array, my issue with my code is it is pushing elements as it was just an array and not object , when i console log it should display it like [{name:"blabla",price:25},{name:"blabla2",price:10}]. Here is my code :

var shoppingCart = [];
function addToCart (itemName,itemPrice) {
  this.name = itemName;
  this.price = itemPrice;
  return shoppingCart.push(itemName,itemPrice)
}
addToCart ("baba",25);
addToCart ("choco",85);
console.log(shoppingCart);

4 Answers 4

2

like this?

var shoppingCart = [];
function addToCart (itemName,itemPrice) {
  shoppingCart.push({name: itemName, price: itemPrice});
}
addToCart ("baba",25);
addToCart ("choco",85);
console.log(shoppingCart);

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

1 Comment

yes but didn't really help me solve my matter below =)
0

have you thought about using a shoppingCart object like this?

var shoppingCart = {
	items: [],
	getItems: function(){
		return this.items;
	},
	add: function(itemName,itemPrice) {
			var newItem = {name: itemName, price: itemPrice};
			this.items.push(newItem);
		}
}

shoppingCart.add("baba",25);
shoppingCart.add("choco",85);

console.log(shoppingCart.getItems());

1 Comment

Yea but it's not relevant to my excercise tu anyways
0

var shoppingCart = [];
function addToCart (itemName,itemPrice) {
  this.name = itemName;
  this.price = itemPrice;
  return shoppingCart.push({name: itemName, price: itemPrice});
}
addToCart ("baba",25);
addToCart ("choco",85);
console.log(shoppingCart);

function priceCheck (itemStore) {
  for (var i=0;i<shoppingCart.length;i++) {
    if (shoppingCart[i].name = itemStore) {
      console.log("The price is " + this.price + " shekels" );
    }
    else {
      console.log("that item is not in your cart");
    }
  }
}
priceCheck ("baba");
console.log(shoppingCart);

function totalPriceCheck () {
  var total = 0;
  for (var i=0;i<shoppingCart.length;i++) {
    total += shoppingCart[i].price;
  }
  return total;
}
totalPriceCheck();

as you can see issue number one is with priceCheck which for some reasons when i console log it gives me baba name twice and thinks baba is 85 instead of 25

second issue is with totalPriceCheck who returns me a NaN when i should i get the sum of the price properties from my array object

Comments

0

RE: your post above, this solves it

var shoppingCart = [];
function addToCart (itemName,itemPrice) {
  this.name = itemName;
  this.price = itemPrice;
  return shoppingCart.push({name: itemName, price: itemPrice});
}
addToCart ("baba",25);
addToCart ("choco",85);
console.log(shoppingCart);

function priceCheck (itemStore) {
  for (var i=0;i<shoppingCart.length;i++) {
    if (shoppingCart[i].name = itemStore) {
      console.log("The price is " + shoppingCart[i].price + " shekels" );
      break;
    }
    else {
      console.log("that item is not in your cart");
    }
  }
}
priceCheck ("baba");
console.log(shoppingCart);

function totalPriceCheck () {
  var total = 0;
  for (var i=0;i<shoppingCart.length;i++) {
    total += shoppingCart[i].price;
  }
  return total;
}
console.log(totalPriceCheck());

3 Comments

As a note; I changed this.price to shoppingCart[i].price and added a break after it found the item in the cart
thanks didn't know at all you could do that with a break , still learning ;)
Ideally you should try to change the priceCheck to use a search rather than iterating over it, i.e. through the use of something like developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/….

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.