简体   繁体   中英

Javascript sub-methods

I'm trying to learn Javascript. I'm making a grocery list that adds, removes, tallies, and clears items. The parts that work are all from previous stack overflow advice. The more I work with it the less I understand why any of it works. I just went through the codeacademy JS track again but it's not matching what I'm doing here.

I'm down to the last two methods: tallying prices & searching for an item to return its properties. When I alter these methods to try and do something simple like print out a number, that won't even work. Ruby was nowhere near this painful to learn. I'll take any advice on understanding JS or how and why my remaining methods are not working.

var groceryList = function(food, quantity, price) {
    this.items = [];
    this.items.push({food:food, quantity:quantity, price:price});

    var currentList = this;
    this.addStuff = function(food, quantity, price) {
        currentList.items.push({food:food, quantity:quantity, price:price});
        return currentList.items;
    };

    this.tallyList = function() {
        return currentList.items.length;
    };

    this.getItems = function() {
        return currentList.items;
    };


// BROKEN - always returns 'not on list' - the matching bit does not work
    this.onList = function(someItem) {
      for(var key in currentList) {
        console.log(currentList[key] === someItem); // t/f
        if(currentList.items === someItem) {
          return currentList[key];
        }
        else {
          return "not on list"
        }
      }
    };

// BROKEN -the simplest of commands here will not print.  WTF?
    this.whatWillItCost = function() {
        mySum = 0;
        for(i = 0; i < currentList.items.length; i++) {
          mySum += currentList.items.price;
        }
        return mySum;
    };

    this.goShopping = function() {
        currentList.items = {};
        return currentList.items;
    };
};



//TESTS

var myList = new groceryList("cookie", 2, 1.00);
console.log(myList);
myList.addStuff("brownie", 1, 2.50);
console.log(myList);
console.log(myList.tallyList());
console.log(myList.onList("cookie")); // must fix - always returns false
console.log(myList.whatWillItCost) // must fix - totally broken
console.log(myList.goShopping());

As for the first function, you are referencing the class instead of the items of the list(which you do well in your second), so it should be:

this.onList = function(someItem) {
  for(i = 0; i < currentList.items.length; i++) {
    if(currentList.items[i].food === someItem) {
      return currentList.items[i]
    }
    else {
      return "not on list"
    }
  }
};

As for your second, only mistake is missing the reference to the index of items list:

mySum += currentList.items[i].price; // should it be * currentList.items[i].quantity?

You also need to change:

console.log(myList.whatWillItCost()) // without the () you are displaying the reference to the function instead of executing it

Js Fiddle: http://jsfiddle.net/3a4ebdpz/

You could also make your onList function a bit shorter this way using Array.filter :

this.onList = function(someItem) {
    var results= currentList.items.filter(function(item){ 
        return item.food===someItem;
    })
    return results.length == 0 ? 'not on list' : results[0]
};

it's working , "I'm down to the last two methods: tallying prices & searching for an item to return its properties" has been fixed.

PFB code ,

<html>
<body>
<script>
var groceryList = function(food, quantity, price) {
    this.items = [];
    this.items.push({food:food, quantity:quantity, price:price});

    var currentList = this;
    this.addStuff = function(food, quantity, price) {
        currentList.items.push({food:food, quantity:quantity, price:price});
        return currentList.items;
    };

    this.tallyList = function() {
        return currentList.items.length;
    };

    this.getItems = function() {
        return currentList.items;
    };


// BROKEN - always returns 'not on list' - the matching bit does not work
    this.onList = function(someItem) {
      for(var key in currentList.items) {
        console.log(currentList.items[key].food === someItem); // t/f
        if(currentList.items[key].food === someItem) {
          return currentList.items[key];
        }
        else {
          return "not on list"
        }
      }
    };

// BROKEN -the simplest of commands here will not print.  WTF?
    this.whatWillItCost = function() {
        mySum = 0;
        for(i = 0; i < currentList.items.length; i++) {
          mySum += currentList.items[i].price;
        }
        return mySum;
    };

    this.goShopping = function() {
        currentList.items = {};
        return currentList.items;
    };
};



//TESTS

var myList = new groceryList("cookie", 2, 1.00);
console.log(myList.items);
myList.addStuff("brownie", 1, 2.50);
console.log(myList.items);
console.log(myList.tallyList());
console.log(myList.onList("cookie")); // must fix - always returns false
console.log(myList.whatWillItCost()) // must fix - totally broken
console.log(myList.goShopping());
</script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM