/    Sign up×
Community /Pin to ProfileBookmark

simple function

Hello,

I have been working on this for awhile trying to understand how to use functions. I am not sure what I am doing wrong, sometimes this works, sometimes it doesn’t. Is there a better way (and still a simple way) to use the function to print out all the input at once?

[CODE]
<HTML>
<HEAD>
<TITLE>SimpleFunction</TITLE>
<SCRIPT TYPE=”text/javascript”>

// Prompt user to enter information
var hoursperweek;
var payperhour;
var taxes;
var totalpay;

</SCRIPT>
</HEAD>
<BODY>
<script type=”text/javascript”>

hoursperweek=prompt(“hours you work per week: “,””);
payperhour=prompt(“Pay per hour: “,””);
taxes=prompt(“Tax rate: “,””);

document.write(“Hours Worked: “+hoursperweek);
document.write(“Hourly Payrate: “+payperhour);
document.write(“Tax rate: “+taxes);

function finalpay(hoursperweek, payperhour, taxes)
{
return var totalpay = (hoursperweek * payperhour) – taxes;
}
document.write(“Net Pay= “+finalpay);

</script>
</BODY>
</HTML>

[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@sstoveauthorFeb 21.2010 — I should add that I am obviously a beginner. I am still having trouble getting the function to work. Any advise would be appreciated. Thank you
Copy linkTweet thisAlerts:
@WestWebFeb 21.2010 — Here is a slightly more simplified way of doing what I think you are trying to accomplish:

[code=html]
<html>
<head>
<title>simple function</title>
<script type="text/javascript">

// Declare Variables and Prompt user to enter information
var hoursperweek;
var payperhour;
var taxes;
var totalpay;

hoursperweek=prompt("hours you work per week: ","");
payperhour=prompt("Pay per hour: ","");
taxes=prompt("Tax rate: ","");

</script>
</head>
<body>
<script type="text/javascript">

document.write("Hours Worked: "+hoursperweek+ " hours <br />");
document.write("Hourly Payrate: $"+payperhour+ " per hour <br />");
document.write("Tax rate: &#37;"+taxes+ "<br />");

var totalpay = ((hoursperweek * payperhour) - taxes);
document.write("Net Pay Per Week = $" + totalpay);

</script>

</body>
</html>
[/code]


*NOTE* That I did not use capital letters in my html tags.
Copy linkTweet thisAlerts:
@toicontienFeb 22.2010 — You could try this:
[CODE]<HTML>
<HEAD>
<TITLE>SimpleFunction</TITLE>
<SCRIPT TYPE="text/javascript">

function calculateAndDisplayFinalPay() {

var hoursPerWeek = prompt("Hours you work per week: ","");
var payPerHour = prompt("Pay per hour: ","");
var taxRate = prompt("Tax rate: ","");
var grossPay = hoursPerWeek * payPerHour;
var taxes = taxRate / 100 * totalPay;
var netPay = grossPay - taxes;

displayFinalPay( hoursPerWeek, payPerHour, taxRate, grossPay, taxes, netPay );

}

function displayFinalPay( hoursPerWeek, payPerHour, taxRate, grossPay, taxes, netPay ) {

var displayMarkup =
"Hours Worked: " + hoursPerWeek + "<br>" +
"Hourly Payrate: " + payPerHour + "<br>" +
"Tax rate: " + taxeRate + "<br>" +
"Net Pay: " + netPay;

var displayNode = document.getElementById( "output" );

if ( displayNode ) {
displayNode.innerHTML = displayMarkup;
}

displayNode = null;

}

[B]window.onload = function() {
calculateAndDisplayFinalPay();
};[/B]

</SCRIPT>
</HEAD>
<BODY>

[B]<p id="output"></p>[/B]

</BODY>
</HTML>[/CODE]
Copy linkTweet thisAlerts:
@sstoveauthorFeb 25.2010 — Those two functions didn't seem to work. As mentioned above I aim is to,

declare var's, done

request input, done

have the function do the math and call the function for output.

My largest problem is the actual coding of the function

Thanks
×

Success!

Help @sstove spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...