Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 15 of 17

01182012, 02:26 AM #1
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
ParceInt, ParceFloat, toFixed ? Trying to get whole number and decimal places.
I'm fairly new to programming, I just started JavaScript and I did read the rules. I am a student and will not post my code asking someone to do my homework. I just need a little guidance.
For my assignment, when a webpage is loaded, it prompts the user to enter hours worked, their pay rate and tax rate, then use a function to calculate the net weekly pay, then display all numbers on the screen. Now I have this working and it appears on the screen, but I need to limit the hours worked to a whole number and the pay and tax rates need to only show 2 digits on either side of the decimal.
From searching I found a lot of information about ParceInt, ParceFloat and toFixed, but cannot get these to work. Either these are not correct or I'm not using them correctly.
Below are samples of 2 of the prompts. Can someone help me with this problem? Also is it better the handle this problem at the prompt or in the function or where it writes to the screen?
Code:var hours_worked = prompt ("How many hours did you work?", ""); parseInt(hours_worked); var pay_rate = prompt ("What is your pay rate?", ""); parseFloat(pay_rate);
BrianLast edited by BBoyd; 01182012 at 03:19 PM.
01182012, 02:38 AM
#2
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,642
 Thanks
 0
 Thanked 649 Times in 639 Posts
parseInt is for converting numbers from one base to another for example parstInt('0xc') will return 12.
parseFloat is one way you could convert the text string input into a number but it disregards any nonnumerics  so parseFloat('2.4getlost') will return 2.4
The first thing you need to do on your input is to validate that it is a number. It can then be converter from a string to a number using Number().
The number of decimal places JavaScript displays on the end of a number when you output it again as text is controlled using toFixed().
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.
Users who have thanked felgall for this post:
BBoyd (01182012)
01182012, 02:47 AM
#3
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
01182012, 03:18 AM
#4
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
I'm missing something, but not sure what. I searched for examples on what you suggested and it looks simply, but I've tried many different ways and it doesn't work.
Here is the last code I tried. How my mind is viewing this problem is that the user is given a prompt to enter the number of hours worked. The assignment states "in whole amounts, no decimals, commas, or $ signs allowed. Like: 40". The user enters say... 38.75 and Number() is suppose to take the string and convert it to a number and then the toFixed Method specifies how many digits are saved after the decimal.
If this thought is correct, how should the code be organized?
Code:var hours_worked = prompt ("How many hours did you work?", ""); Number(hours_worked); hours_worked.toFixed(0);
01182012, 04:03 AM
#5
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
I've been working on this longer than needed, but I have to figure it out. With a lot of trial and error, I have it working, but there is a lot more code than I think there should be.
Can someone help me understand a better way with less code?Code:var hours = prompt ("How many hours did you work?", ""); var hours2 = parseFloat(hours); var hours3 = hours2.toFixed(0);
01182012, 04:25 AM
#6
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
I will need to give this subject a break and will return to it in the morning. 3+ hours on this has put me behind schedule.
Another note before logging off. The toFixed() sets the amount of digits of the right side of the decimal. How can you specify how many digits to the left side of the decimal? Another part of the assignment states "in decimal format with up to two digits to the left and two digits to the right of the decimal point", which would eliminating someone putting in a hourly pay rate of over $100.
BTW... I read the rules about homework assignments and I feel that I have followed the rules and shown that I'm trying to work through the problem myself. I'm not trying to get someone else to do my homework so hopefully some experiences programmers will help guide me. I'm 43 years old and back in school taking 6 classes so the time I've spent on this one problem hurts. I could easily submit the assignment and get a 90, but I truly want to understand and learn this.
Thanks for your time.
Brian
01182012, 08:24 AM
#7
 Join Date
 Dec 2010
 Posts
 2,398
 Thanks
 12
 Thanked 570 Times in 563 Posts
Don't worry about that rule ... you presented enough effort
But: You'll have to understand the difference between converting a number and validating a number. If the requirement says "no more than two digits on the left", it will have influence on the validity of the number. You have to check validity before converting the number.
Example: User input $120.95 ... according to the requirements, this is an illegal number. If you just convert it to "two digits left and two digits right of the decimal point" you will get into trouble.
Code:var hours = 1; while(hours<0  hours>100) { var hours = parseFloat(prompt ("How many hours did you work?", "")); if(hours>100) alert('too many hours'); } // at this point the hours are valid // convert toFixed hours = hours.toFixed(0);
01182012, 08:31 AM
#8
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,310
 Thanks
 203
 Thanked 2,563 Times in 2,541 Posts
.toFixed() changes the number into a string value, and so is suitable only for display purposes. You cannot perform arithmetic calculations on a string value.
To round a number to x decimal places, while still retaining the number type
Code:<script type = "text/javascript"> function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; } alert (roundNumber(123.45678,2)); </script>
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
BBoyd (01182012)
01182012, 08:37 AM
#9
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,642
 Thanks
 0
 Thanked 649 Times in 639 Posts
can be shortened toCode:function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; }
Also you are better off defining functions usingCode:function roundNumber(num, dec) { return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); }
as the other way around doesn't work in all situations.Code:roundNumber = function(num, dec) { return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); }
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.
01182012, 08:52 AM
#10
01182012, 09:02 AM
#11
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,642
 Thanks
 0
 Thanked 649 Times in 639 Posts
for example it doesn't work inside an if statement
will not work because JavaScript does not specify what value the function should have if defined that way. In browsers complying most closely to the standard that would be a syntax error. Other browsers might always run the first of the two function declarations regardless of the if condition and others the second. Some will just treat it all as the equivalent of alert(undefined).Code:if (some_condition) { function x() {return 'a';} } else { function x() {return 'b';} } alert(x());
Define the function the other way around and you gain better control of where the function applies and can define alternatives in an if statement.
As another example you can't rewrite a function defined that way and so cannot for example would not be able to include the above code with either style of function declaration inside of function x();
is also invalid but would be valid if the outer function were also defined the other way around.Code:function x() { if (some_condition) { x = function() {return 'a';} } else { x = function() {return 'b';} } return x(); } alert(x());
There are further instances where placing function first in the declaration doesn't work but the above are the two that I am most familiar with.
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.
01182012, 09:07 AM
#12
01182012, 11:15 AM
#13
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
Thanks to all of you for your help!
I really appreciate the help you have giving me and one day I will pay it forward. Some of this is a little beyond me, but I will come back and visit this thread at a later date when it might make more sense.
The only issue that remains is stripping any characters that are not numbers from what the user enters in the prompt. I tested the page and entered $40 in the hours worked and the display was NaN as well as the calculation amount that used this number.
Below is the code so far that all of you helped me with. I will be submitting 2 files for my assignment and hopefully the instruction will give me credit for this one. If not, what I did by myself should still be good enough for an 'A'.
This assignment is not due until Feb 19th, but I have a normal job while taking 6 classes this semester and so I need to work my butt off to get as far ahead as possible because the assignments are starting to take longer.
I will be back as I'm thirsty for knowledge so please remember me.
Code://I'm submitting 2 documents because some of the code/knowledge found in this assignment //was obtained at... //http://www.codingforums.com/showthread.php?t=249129 //if this is not acceptable, the file (chap4assign1MyOwnWork.html) is my own work. //function to round numbers to specified amount that can be used in calculations. //I learned more about math.round and math.pow on page 313315 in the textbook. function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; } //calculate gross pay & tax amount, then subtract tax amount from gross pay for net pay function calculate_pay(hours,pay,tax) {SNIP}... //ask for hours worked var hours = 1; while(hours<0  hours>100) { var hours = parseFloat(prompt ("How many hours did you work?", "")); if(hours>100) alert('too many hours'); } //at this point the hours are valid. round number to required decimal place. hours = roundNumber(hours,0);
01182012, 11:29 AM
#14
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,310
 Thanks
 203
 Thanked 2,563 Times in 2,541 Posts
The numbers after the decimal point are of course decimal fractions of an hour, and not minutes.Code:<script type = "text/javascript"> var hours = parseFloat(prompt("Please enter your hours worked","")); while (isNaN(hours)  hours < 0  hours > 100) {hours = parseFloat(prompt("Please enter your hours worked  numbers only max.100",""))} alert (hours.toFixed(2)); // for testing  result is a string alert (roundNumber(hours,2)); // for testing  result is a number function roundNumber(num, dec) { return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); } </script>
13.2 hours means 13 hours 12 minutes, not 13 hours 20 minutes.
hours = roundNumber(hours,0); is pointless  if you only want to use whole numbers of hours use parseInt() to start with.
But if you do use parseInt() you must remember to specify the radix  parseInt(mynumber,10) where 10 means decimal.
Otherwise if you user enters a leading zero the number (say 014) will be interpreted as octal.
Last edited by Philip M; 01182012 at 12:00 PM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
BBoyd (01182012)
01182012, 11:46 AM
#15
 Join Date
 Jan 2012
 Location
 Charleston, SC
 Posts
 23
 Thanks
 9
 Thanked 0 Times in 0 Posts
That's awesome Phillip and it works great.
I can read the textbook and follow along as if it all make sense, but then when I start working on an assignment, I'm like a deer in headlights. It is only during conversations like this in forums that I really learn from. This is my first thread here, but have started threads on other sites and the knowledge one can learn from others is just priceless.
Back to reading the next chapter. Javascript: A Beginners Guide and I'm on chapter 5 that covers operators so I will be back probably next week. Tomorrow I will move on to other subjects for about a week. I spend 13 days on a subject before putting it down. The benefit of working around the clock and getting ahead is being about to spend some continuous time on a subject.
Have a wonderful day!
Brian