0

I'm trying to learn JavaScript and am going through an exercise where I'm creating a grocery list that populates with a food, quantity, and cost. I cannot seem to pass in multiple variables or make an array of arrays. I tried some other options like "new Object" but I can't get anything off the ground. Give me a clue?

var groceryList = function(food, quantity, price) {
  var theItem = [food, quantity, price]
  var theList = new Array();
  theList.push(theItem)

}


myList = new groceryList("cookie", 2, 1.00)
console.log(myList)
1
  • 1
    The problem is that you aren't doing anything with theList. You're just creating it, but not returning it. Commented Oct 14, 2014 at 19:58

3 Answers 3

5

Use this

var groceryList = function(food, quantity, price) {
  var theItem = [food, quantity, price]
  var theList = new Array();
  theList.push(theItem);

  return theList;
}

myList = new groceryList("cookie", 2, 1.00)
console.log(myList)
Sign up to request clarification or add additional context in comments.

3 Comments

You could just return return [theItem] which would return an array instead of using the member methods of the Array object
Yeah, that would return an Array with an Array in it. Why would you want that?
you can try for cycle for that
0

If you want to use objects, then you need to change your thinking a little bit. When you create an object with new then the constructor gets called.

function GroceryList(food, quantity, price) {
    this.food = food;
    this.quantity = quantity;
    this.price = price;
}
GroceryList.prototype.toString = function() {
  return this.food + (this.quantity).toString() + (this.price).toString();
}

// lazy array syntax
var GroceryListPool = [];


// popular the array list pool
var list1 = new GroceryList("Butter", 2, 3.999);


GroceryListPool.push(list1);

To iterate the GroceryListPool array:

for(var i = 0; i < GroceryListPool.length; i++) {
    var list = GroceryListPool[i];
    // list is an object of type GroceryList
    // technically it is not a "type", but you know what I mean.
    alert(list);
}

Comments

0

That's not even really a Constructor, yet. Check this out.

function groceryList(food, quantity, price){
  this.items = {};
  if(food !== undefined){
    this.items[food] = {quantity:quantity, price:price, total:quantity*price};
  }
  this.addItem = function(food, quantity, price){
    this.items[food] = {quantity:quantity, price:price, total:quantity*price};
  }
  this.getFood(food){
    return this.items[food];
  }
  this.getQuantity = function(food){
    return this.items[food].quantity;
  }
  this.getTotal = function(food){
    return this.items[food].total;
  }
  this.getItemsByPrice(low, high){
    var r = {}, t = this.items;
    for(var i in t){
      var f = t[i], p = f.price;
      if(p >= low && p <= high){
        r[i] = f;
      }
    }
    return r;
  }
}
var groc = new groceryList('potato', 4, 0.89);
groc.addItem('orange', 10, 1);
console.log(groc.getQuantity('potato'));
console.log(groc.getTotal('orange'));
console.log(groc.getFood('orange').price);
// same as
console.log(groc.getPrice('orange'));
// or
console.log(groc.items.orange.price);
groc.addItem('pear', 200, 0.75);
console.log(groc.getItemsByPrice(0.25, 0.99)); // should be Object with 'potato' and 'pear'

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.