Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Rachel Miller
4,804 PointsCan't figure out a portion of JavaScript code. Any suggestions?
I'm trying to compare prices at two places I shop for food. I can't figure out how to code the area I commented out. Any thoughts? Thanks!
var hyveeList = {};
var coopList = {};
// while (item(s) in hyveeList and coopList are the same) {
// if (hyvee price > coop price) {
// return hyvee price - coop price;
// } else if (coop price > hyvee price) {
// return coop price - hyvee price;
// } else {
// console.log("Same Price!");
// }
function input(location, food, freshness, price) {
if(location.toLowerCase() === "hyvee") {
hyveeList[food] = {freshness: freshness, price: price};
} else if(location.toLowerCase() === "coop") {
coopList[food] = {freshness: freshness, price: price};
} else {
console.log("The location you selected is not a part of this program");
}
}
1 Answer
LaVaughn Haynes
12,397 Pointshere is one way that you could approach it
var hyveeList = {};
var coopList = {};
var results = '';
//some data to work with
var foods = [
['hyvee', 'bread', 'fresh', 2.00],
['hyvee', 'peanut butter', 'fresh', 3.00],
['hyvee', 'jelly', 'fresh', 2.50],
['hyvee', 'apple juice', 'fresh', 5.00],
['coop', 'bread', 'fresh', 2.50],
['coop', 'peanut butter', 'fresh', 3.50],
['coop', 'jelly', 'fresh', 2.50],
['coop', 'apple juice', 'fresh', 4.75]
];
//your original function
function input(location, food, freshness, price) {
if(location.toLowerCase() === "hyvee") {
hyveeList[food] = {location: location, freshness: freshness, price: price};
} else if(location.toLowerCase() === "coop") {
coopList[food] = {location: location, freshness: freshness, price: price};
} else {
console.log("The location you selected is not a part of this program");
}
}
//loop through the data and send it to your function
function makeLists(foodArray){
for(var i = 0, l = foodArray.length; i < l; i++){
input(foodArray[i][0], foodArray[i][1], foodArray[i][2], foodArray[i][3]);
//can also do the following way if you undertsand what's going on:
//window.input.apply(window, foodArray[i]);
}
}
//compare foods in the lists
function compareLists(list1, list2){
//if you return in a loop it will stop the loop,
//so push the value into an array and return the array when loop is done
var listResults = [];
//loop through list 1 (assumes both lists contains same food)
for(var food in list1){
//verify food also exists in list 2
if(list2[food]){
if(list1[food].price > list2[food].price){
//list 1 is more expensive. Get price to 2 decimal points
listResults.push( list2[food].location + ' ' + food +' is cheaper by $' + (list1[food].price - list2[food].price).toFixed(2));
}else if(list1[food].price < list2[food].price){
//list 2 is more expensive. Get price to 2 decimal points
listResults.push( list1[food].location + ' ' + food +' is cheaper by $' + (list2[food].price - list1[food].price).toFixed(2));
}else{
//same price in both lists
listResults.push('At both stores ' + food + ' is the same price');
}
}
}
//returns array of values
return listResults;
}
//Do the work
makeLists(foods);
results = compareLists(hyveeList, coopList);
console.log(results.join(', '));
Rachel Miller
4,804 PointsRachel Miller
4,804 PointsThanks for the insight :) I appreciate you answering in such detail, it's very helpful!