This is a discussion on "Benbacardi's code logic" within the JavaScript Forum section. This forum, and the thread "Benbacardi's code logic are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Benbacardi's code logic
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
Benbacardi's code logic
I'm interested in the logic behind a section of code Benbacardi posted in answer to one of my questions some weeks ago. I wonder if anyone would care to explain it so i can understand what's going on? Firstly here is the code:
Also, I notoced Benbacardi started off with declaring the variable = 0. This worked well but I though that the only time a variable needed to be initialised was within a for loop. What is the significance of declaring it as = 0 here, in the context of this piece of code rather than simply creating an empty variable which allows the user to input their own value? netwarriorgizmo |
|
|
|
|||
|
ParseInt tries to convert a value to an integer and parsefloat tries to convert to a float.
like "age5.3" would convert to "5" with parseint, and "5.3" with parsefloat. The initialization of the variable is because it's being used in the conditional of the while loop. The first time through the while it needs a value that will fail the while comparison so that it asks for the age at least once. A null value and empty aren't the same thing, null stands for value unknown, so comparing things to null doesn't always work the way you think it might. |
|
|||
|
This is interesting. In the Introduction to JavaScript course I've done ParseFloat is used exclusively to turn numerical information into number representations when ParseInt would be more appropriate. I've learned something here then.
Not too sure I'm grasping the significance of the 0 value though. If this initialises the variable to 0 value I don't understand why the loop doesn't produce the same results (based on a null value) by follwign the if logic netwarriorgizmo |
![]() |
| Tags |
| benbacardis, code, logic |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Logic Problem involving loops and arrays | tox0tes | Other Programming Languages | 5 | Oct 28th, 2007 03:11 |
| Anybody use flow charts for logic design? | Donny Bahama | PHP Forum | 3 | May 8th, 2007 15:44 |
| php While logic | dcarson | PHP Forum | 3 | Feb 7th, 2007 18:46 |
| Logic help | jayaime | JavaScript Forum | 4 | Aug 31st, 2006 14:28 |
| Can somebody give me the code to hide the source code? | renren | JavaScript Forum | 7 | Mar 7th, 2006 12:27 |