-1

I have a javascript function that is returning a value. However, I can not get the value to return outside of the function. I have researched and can not figure out the issue. It says grossResults in the last line of code is undefined. Wouldn't the return make it accessible?

"use script";

var hourlyRate = prompt("Enter hourly pay rate:", hourlyRate);
var totalHrsWorked = parseInt(prompt("Enter number of hours worked:", totalHrsWorked));


//function to calculate gross wages for an hourly employee
function grossWages (hourlyRate, totalHrsWorked){

var OTHours = totalHrsWorked - 40;
var regPay = (totalHrsWorked - OTHours) * hourlyRate;
var OTPay = (OTHours * (hourlyRate * 1.5)) + regPay;
grossResults = (OTHours > 0) ? OTPay : regPay;
return grossResults;
};


document.writeln("<br /><br />");
document.writeln("Pay rate entered: " + hourlyRate + "<br />");
document.writeln("Hours entered: " + totalHrsWorked  + "<br />");

document.writeln("Gross Pay: $" + grossResults + "<br /><br />")   
user2448412
  • 75
  • 2
  • 11
  • 3
    Where are you calling `grossWages`? You merely defined the function, you didn't call it. The code inside the function got never executed. I recommend to read a [**JavaScript tutorial**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) before you proceed further, especially [about functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions). – Felix Kling Oct 02 '14 at 20:19
  • 1
    I believe the last line was meant to be: `document.writeln("Gross Pay: $" + grossWages(hourlyRate, totalHrsWorked ) + "

    ") `
    – LcSalazar Oct 02 '14 at 20:26

2 Answers2

1

As mentioned in the comments, you aren't invoking the function. Javascript has support for first-class functions, which means you can use functions like strings/numbers and pass around their reference, which is why this doesn't error out when its run.

document.writeln("Gross Pay: $" + grossResults(hourlyRate, totalHrsWorked) + "<br /><br />")

The above code properly calls your functions, with your arguments you pulled in from the prompt!

Brent Echols
  • 590
  • 2
  • 9
0

There's a few problems.

First is understanding scope in javascript. JS has function scope, which basically means variables (and functions) declared inside a function are only visible inside that function (incl. its children). For more detail, see for ex. What is the scope of variables in JavaScript?. I'm skipping all about inheritance here, which works by prototypes, but it basically just defines hierarchical relationships between objects (so they can see variables/functions their parents have).

What scope means in your example's context is that grossResults is defined inside function grossWages so it's not visible outside that function. When function grossWages gets called (which never happens in your example, but what you're trying to achieve is probably var grossResults = grossWages() - in which case grossWages() returns the value of its variable grossResults, it doesn't expose that variable to the context outside that function.

Notes: "use script" probably should be "use strict". See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

Community
  • 1
  • 1
Marcus
  • 5,083
  • 3
  • 32
  • 39