I have an excel file with data like this
Name | WorkerId | ManagerId |
---|---|---|
Tom | 179 | 180 |
Liz | 150 | 179 |
Ricki | 120 | 179 |
sona | 113 | 150 |
Preet | 558 | 150 |
mina | 89 | 558 |
Yukti | 45 | 120 |
And I want a function CountEmployee(manager_id) that will return all the employees under him. For example:
CountEmployee(179) = 6 , CountEmployee(150) = 3
I am using papa parse library to parse this excel into object, and wrote an recursive function to get the total employee.
parseCsv to parse the csv
function parseCsv() {
return new Promise((resolve, reject) => {
Papa.parse("Employees.csv", {
download: true,
complete: function (results) {
return resolve(results);
},
});
});
}
CountDirectEmployees to get the Direct employees of a manager Id
async function CountDirectEmployee(data) {
var countDirect1 = 0
const results = await parseCsv();
results.data.map((row, index) => {
if (row[2] == data) {
countDirect1 = countDirect1 + 1
}
})
return countDirect1;
}
And finally,
The CountEmployee should return the final count
async function CountEmployee(data,count) {
const results = await parseCsv();
CountDirectEmployee(data).then(
function(count1){
results.data.forEach((row, index) => {
if (row.ManagerId == data) {
count1 = count+count1
CountEmployee(row[1],count1)
}
})
}
)
return count
}
I know my logic is wrong for CountEmployee function, somewhere. Not able to figure out the issue.