Duvall912 Duvall912 - 1 month ago 15
Javascript Question

depName is not defined

It tells me that depName is undefined.I am trying to iterate,just can see whats wrong with it.I am trying to get the most profitable department out of the array.

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 depMap = {};
function mostProfitableDepartment() {
for (var i = 0; i < depName.length; i++) {
var currentDep = depName[i]
}
console.log(currentDep);
}
mostProfitableDepartment(salesData);

Answer

You forgot to set a parameter on your function mostProfitableDepartment

// missing depName in function declaration
function mostProfitableDepartment(depName) {
    for (var i = 0; i < depName.length; i++) {
        var currentDep = depName[i];
        console.log(currentDep); // Was outside of the loop
    }

}