PDA

View Full Version : Problem with javascript output



maroonmb
Sep 5th, 2014, 06:39 PM
Hi,
I have a problem with document.write output, it's just don't display the result in the browser but the prompt and everything else working fine. I explain the idea, it's a small tax calculator, you add the hours per day and number of days per week and the calculator diplay in the web page the result. here is my code thats not working:

<html>
<body>
<head>
<title>Tax Calculator</title>
</head>
<h1>Tax Calculator</h1>
<script type="text/javascript">
var dailyHours = prompt ("How many hour you work per day?");
var weeklyHours = prompt ("How may days per week your working?");
var tax = 0.5;
var hourRate = 8;
var monthWage = dailyHours*weeklyHours;
documemt.write('Your Monthly salary before tax ' + monthWage);
documemt.write("Your monthly wage after Tax is " = monthWage*0.5);



</script>


</body>
</html>

Shaka Zorba
Sep 5th, 2014, 06:54 PM
<html>
<body>
<head>
<title>Tax Calculator</title>
</head>
<h1>Tax Calculator</h1>
<script type="text/javascript">
var dailyHours = prompt ("How many hour you work per day?");
var weeklyHours = prompt ("How may days per week your working?");
var tax = 0.5;
var hourRate = 8;
var monthWage = dailyHours*weeklyHours;
document.write('Yourthly salary before tax ' + monthWage);
document.write("Yourhly wage after Tax is " + monthWage*0.5);
</script>
</body>
</html>

Philip M
Sep 5th, 2014, 07:35 PM
Be aware that prompts, alerts and document.write() are all regarded as obsolete since Netscape 3 passed away 10+ years ago. You should use DOM methods to obtain input from and display messages to your users.

maroonmb
Sep 5th, 2014, 08:21 PM
<html>
<body>
<head>
<title>Tax Calculator</title>
</head>
<h1>Tax Calculator</h1>
<script type="text/javascript">
var dailyHours = prompt ("How many hour you work per day?");
var weeklyHours = prompt ("How may days per week your working?");
var tax = 0.5;
var hourRate = 8;
var monthWage = dailyHours*weeklyHours;
document.write('Yourthly salary before tax ' + monthWage);
document.write("Yourhly wage after Tax is " + monthWage*0.5);
</script>
</body>
</html>

thank you :)

maroonmb
Sep 5th, 2014, 08:30 PM
Be aware that prompts, alerts and document.write() are all regarded as obsolete since Netscape 3 passed away 10+ years ago. You should use DOM methods to obtain input from and display messages to your users.


Just started studying javascript and our tutor start straight away with these codes, but I will come back here for advice's and assistance.
Thank you

Old Pedant
Sep 5th, 2014, 09:53 PM
And youre answer is WRONG WRONG WRONG.

These two lines contain MAJOR mistakes:
var monthWage = dailyHours*weeklyHours;
document.write("Yourhly wage after Tax is " + monthWage*0.5);

Philip M
Sep 6th, 2014, 07:51 AM
And youre answer is WRONG WRONG WRONG.

These two lines contain MAJOR mistakes:
var monthWage = dailyHours*weeklyHours;
document.write("Yourhly wage after Tax is " + monthWage*0.5);

Perhaps I am blind, but I do not see the (Javascript) mistakes. Of course, the code is very primitive with logic errors but it does give the answer if the user enters numbers. The OP seems to be confused between weekly and monthly wage. And the tax is not explicitly shown.

Philip M
Sep 6th, 2014, 07:53 AM
Just started studying javascript and our tutor start straight away with these codes, but I will come back here for advice's and assistance.
Thank you

Tell your tutor that it is a waste of time to learn stuff which was obsolete years ago.

Old Pedant
Sep 6th, 2014, 07:42 PM
*sigh*

var monthWage = dailyHours*weeklyHours;
document.write("Yourhly after Tax is " + monthWage*0.5);

should be:


var monthWage = dailyHours*weeklyHours * hourRate;
document.write(""
+ " after tax is " + monthWage * ( 1 - tax );

Granted, multiplying by 0.5 happens to be the same as multiplying by 1-tax, but only because the tax rate is 50%. Presumably, he should be using coding, not calculating numbers in his head, to arrive at the right result.

NOTE: Sorry for the weird looking document.write. This forum EATS text in some conditions. It was converting "Net salary is" to "Netry is" (for example).

Philip M
Sep 7th, 2014, 04:18 PM
*sigh*

var monthWage = dailyHours*weeklyHours;
document.write("Yourhly after Tax is " + monthWage*0.5);

should be:


var monthWage = dailyHours*weeklyHours * hourRate;
document.write(""
+ " after tax is " + monthWage * ( 1 - tax );

Granted, multiplying by 0.5 happens to be the same as multiplying by 1-tax, but only because the tax rate is 50%. Presumably, he should be using coding, not calculating numbers in his head, to arrive at the right result.

NOTE: Sorry for the weird looking document.write. This forum EATS text in some conditions. It was converting "Net salary is" to "Netry is" (for example).

Yes, I said that there were logic errors such as those you point out, but these are not Javascript syntax errors. :)
His big Javascript syntax problem was mis-spelling document (keyboard mistype) and using an = (another keyboard mistype) instead of a +.

Part of his trouble is assigning an inappropriate or misleading name to a variable:- var weeklyHours = prompt ("How may days per week your working?"); Better would be weeklyDays or daysPerWeek. There are many logic or validation errors - what if someone claims to work 96 hours a day for 15 days a week?