Duvall912 Duvall912 - 26 days ago 7
Javascript Question

Having trouble,it doesn't return the right for in value

The

for in
should display
Friday
as the most profitable day in the week but it returns all the other days of the week in the object map except Friday.

var salesData = [
{department : 'hardware', sales : 4500, day : 'Monday'},
{department : 'outdoor', sales : 1500, day : 'Monday'},
{department : 'carpentry', sales : 5500, day : 'Monday'},
{department : 'hardware', sales : 7500, day : 'Tuesday'},
{department : 'outdoor', sales : 2505, day : 'Tuesday'},
{department : 'carpentry', sales : 1540, day : 'Tuesday'},
{department : 'hardware', sales : 1500, day : 'Wednesday'},
{department : 'outdoor', sales : 8507, day : 'Wednesday'},
{department : 'carpentry', sales : 8009, day : 'Wednesday'},
{department : 'hardware', sales : 12000, day : 'Thursday'},
{department : 'outdoor', sales : 18007, day : 'Thursday'},
{department : 'carpentry', sales : 6109, day : 'Thursday'},
{department : 'hardware', sales : 7005, day : 'Friday'},
{department : 'outdoor', sales : 12006, day : 'Friday'},
{department : 'carpentry', sales : 16109, day : 'Friday'},
];

var dayForEachDepartment = {};

function mostProfitableDay(weekdays){
(var i = 0; i < weekdays.length; i++) {

var profitableDay = weekdays[i]

if ( dayForEachDepartment[profitableDay.day] === undefined){
dayForEachDepartment[profitableDay.day] = 0;
}

dayForEachDepartment[profitableDay.day] += (profitableDay.sales);
}
}
mostProfitableDay(salesData);
//ObjectMap holding the day's most sold on that specific day.
var daySoldMost ={
day : '',
sales: 0
};

for (var daysWithSales in dayForEachDepartment){
if (dayForEachDepartment[daysWithSales] > daySoldMost.sales) {
daySoldMost.day = daysWithSales;
daySoldMost.sales = dayForEachDepartment[daysWithSales]
}
console.log(daySoldMost);
}

Answer

If you move console.log(daySoldMost); outside of the for loop, you get the wanted result. BTW, thursday is the one with the most sales.

var salesData = [{ department: 'hardware', sales: 4500, day: 'Monday' }, { department: 'outdoor', sales: 1500, day: 'Monday' }, { department: 'carpentry', sales: 5500, day: 'Monday' }, { department: 'hardware', sales: 7500, day: 'Tuesday' }, { department: 'outdoor', sales: 2505, day: 'Tuesday' }, { department: 'carpentry', sales: 1540, day: 'Tuesday' }, { department: 'hardware', sales: 1500, day: 'Wednesday' }, { department: 'outdoor', sales: 8507, day: 'Wednesday' }, { department: 'carpentry', sales: 8009, day: 'Wednesday' }, { department: 'hardware', sales: 12000, day: 'Thursday' }, { department: 'outdoor', sales: 18007, day: 'Thursday' }, { department: 'carpentry', sales: 6109, day: 'Thursday' }, { department: 'hardware', sales: 7005, day: 'Friday' }, { department: 'outdoor', sales: 12006, day: 'Friday' }, { department: 'carpentry', sales: 16109, day: 'Friday' }, ];

var dayForEachDepartment = {};

function mostProfitableDay(weekdays) {
    for (var i = 0; i < weekdays.length; i++) {
        var profitableDay = weekdays[i]
        if (dayForEachDepartment[profitableDay.day] === undefined) {
            dayForEachDepartment[profitableDay.day] = 0;
        }
        dayForEachDepartment[profitableDay.day] += (profitableDay.sales);
    }
}
mostProfitableDay(salesData);
//ObjectMap holding the day's most sold on that specific day.
var daySoldMost = {
    day: '',
    sales: 0
};

for (var daysWithSales in dayForEachDepartment) {
    if (dayForEachDepartment[daysWithSales] > daySoldMost.sales) {
        daySoldMost.day = daysWithSales;
        daySoldMost.sales = dayForEachDepartment[daysWithSales]
    }
}
console.log(dayForEachDepartment);
console.log(daySoldMost);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Bonus, a more concise solution with a single loop.

function mostProfitableDay(weekdays) {
    weekdays.forEach(function (day, i) {
        dayForEachDepartment[day.day] = (dayForEachDepartment[day.day] || 0) + day.sales;
        if (!i || dayForEachDepartment[day.day] > dayForEachDepartment[daySoldMost]) {
            daySoldMost = day.day;
        }
    });
}

var salesData = [{ department: 'hardware', sales: 4500, day: 'Monday' }, { department: 'outdoor', sales: 1500, day: 'Monday' }, { department: 'carpentry', sales: 5500, day: 'Monday' }, { department: 'hardware', sales: 7500, day: 'Tuesday' }, { department: 'outdoor', sales: 2505, day: 'Tuesday' }, { department: 'carpentry', sales: 1540, day: 'Tuesday' }, { department: 'hardware', sales: 1500, day: 'Wednesday' }, { department: 'outdoor', sales: 8507, day: 'Wednesday' }, { department: 'carpentry', sales: 8009, day: 'Wednesday' }, { department: 'hardware', sales: 12000, day: 'Thursday' }, { department: 'outdoor', sales: 18007, day: 'Thursday' }, { department: 'carpentry', sales: 6109, day: 'Thursday' }, { department: 'hardware', sales: 7005, day: 'Friday' }, { department: 'outdoor', sales: 12006, day: 'Friday' }, { department: 'carpentry', sales: 16109, day: 'Friday' } ],
    dayForEachDepartment = {},
    daySoldMost,
    daySoldMostObject;

mostProfitableDay(salesData);

daySoldMostObject = {
    day: daySoldMost,
    sales: dayForEachDepartment[daySoldMost]
};

console.log(dayForEachDepartment);
console.log(daySoldMost);
console.log(daySoldMostObject);
.as-console-wrapper { max-height: 100% !important; top: 0; }