In: Computer Science
Please see if you can correct my code. I am getting an ReferenceError in Windows Powershell that says payment is undefined. I am trying to create a main.js file that imports the function from the hr.js file; call the function passing the necessary arguments and log the result to the console.
main.js
var Dev = require("./hr.js")
const { add } = require("./hr.js")
var dev_type = 1;
var hr = 40;
console.log("your weekly payment is " + payment(dev_type, hr))
dev_type = 2;
hr = 45;
console.log("your weekly payment is " + payment(dev_type, hr));
hr.js
function payment(dev_type, hr) {
var total_payment;
var extra_hr;
if (dev_type == 1) {
if (hr <= 40) {
total_payment = hr * 55;
} else {
extra_hr = hr - 40;
total_payment = 40 * 55 + (extra_hr * 110);
}
}
if (dev_type == 2) {
if (hr <= 40) {
total_payment = hr * 60;
} else {
extra_hr = hr - 40;
total_payment = 40 * 60 + (extra_hr * 120);
}
}
return total_payment;
}
module.exports = {add: payment}
IN YOUR CODE, YOU HAVE IMPORTED AND EXPORTED "PAYMENT" FUNCTION AS "ADD" BUT WHEN YOU CALL THAT FUNCTION YOU HAVE USED THE PAYMENT KEYWORD WHICH IS NOT DECLARED IN MAIN.JS (REFERENCE ERROR:)
YOU CAN EITHER IMPORT THE FUNCTION AS "PAYMENT" OR CALL THAT FUNCTION WITH THE KEYWORD "ADD"
#SOLUTION 1
IMPORT AND EXPORT THE FUNCTION AS "PAYMENT"
IN hr.js
CHANGE
TO
module.exports = {payment}
IN main.js
CHANGE
TO
const { payment } = require("./hr.js")
#SOLUTION 2
CALL THE FUNCTION BY USING THE KEYWORD "ADD"
IN main.js
CHANGE
TO
console.log("your weekly payment is " + add(dev_type, hr))
console.log("your weekly payment is " + add(dev_type, hr));
COMMENT BELOW IF HAVING ANY DOUBTS