0

I have a problem in JS that really burns in my soul!! I want to create a program that prompt the user to inter his name then his hourly wage then how many hours he has worked for? like: Enter your name please: Saleh Enter your hourly wage: 20 Enter the worked hours: 8

I need this program to print a message that says: Hello Saleh Your hourly wage is: 20 $ You have worked for: 8 hours Your payment is: 160 $

the program that i have made is working but the last line isn't giving any calculation! it prints NaN !!

Please help me

here's the code:

function Worker(aName, anHourlyWage, numberOfHours) // initializing the worker function that asked in the question.
{
  this.name = aName;
  this.anHourlyWage = anHourlyWage;
  this.numberOfHours = numberOfHours;

  this.setHourlyWage = setHourlyWage;
  this.getName = getName;
  this.getHourlyWage = getHourlyWage;
  this.getHours = getHours;
  this.calculatePayment = calculatePayment;
  this.printDetails = printDetails;


}

function setHourlyWage(setHourlyWage) {
  this.anHourlyWage = anHourlyWage2;
}

function getName() {
  return this.name;
}

function getHourlyWage() {
  return this.anHourlyWage;
}

function getHours() {
  return this.numberOfHours;
}

function calculatePayment() {
  return this.getHourlyWage * this.getHours; //to return the multiplication of working hours function and  hourly wage function
}

function printDetails() {
  window.alert("Hello: " + this.getName() +
               "\n Your Wage Is: " + this.getHourlyWage() +
               " $" + "\n You Have Worked For: " + this.getHours() + " Hours" +
               "\n Your Payment Is: " + this.calculatePayment() + " $");
}


var name1 = window.prompt("Enter your name:", "");
var anHourlyWage1 = parseFloat(window.prompt("Enter the Hourly Wage:"));
var numberOfHours1 = parseFloat(window.prompt("Enetr the working hours:"));

var Worker1 = new Worker(name1, anHourlyWage1, numberOfHours1);
Worker1.printDetails();

4
  • what do you think this is in setHourlyWage, getName etc? Commented Dec 11, 2017 at 1:58
  • @JaromandaX - It's the right object, isn't it, as set by the initial Worker1.printDetails() call? Commented Dec 11, 2017 at 2:03
  • yes, so it is. Never seen it done that way before. Ugly Commented Dec 11, 2017 at 2:05
  • @JaromandaX—it's like prototype inheritance without the prototype. Or inheritance… ;-) Commented Dec 11, 2017 at 3:27

1 Answer 1

1

The problem is that in your calculatePayment() function you are not calling the getHourlyWage() and getHours() functions and multiplying their return values, you are multiplying references to the functions themselves.

Change this line:

return this.getHourlyWage * this.getHours;

to this:

return this.getHourlyWage() * this.getHours();

(Note the added parentheses.)

Sign up to request clarification or add additional context in comments.

1 Comment

I think this falls in the category of typo, so voted to close too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.