/**
* This function calculates the cost of an employee based on the data in its file
* @param {string} filePath - path to the employee data file
* @returns {{name: string, cost: number}} - the name and cost of the employee
*/
function costCalculator(filePath) {
const fs = require("fs");
fs.readFile(`${filePath}`,"utf8",(err,data)=>{
if(err){
console.error(err);
return;
}
var employee=JSON.parse(data);
let cph=employee["salary"]/employee["hours"];
var result={name:employee["name"],cost:cph};
console.log(result);
});
}
console.log(costCalculator('mohamed.json'));
it returns
undefined
{ name: 'Mohamed', cost: 62.5 }
when I expect only
{ name: 'Mohamed', cost: 62.5 }