Benbacardi's code logic

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 28th, 2004, 23:25
Junior Member
Join Date: Jul 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
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:
Code: Select all
age = 0

while (parseInt(age) < 5){
    age = window.prompt('Please enter your age','');
}

if (age >59) {
    document.write ('The price of your ticket is a concessionary £8');
} else {
    document.write ('The price of your ticket is £10');
}
I'm interested in two specifics here. Firstly can anyone explain the differences between using the functions ParsFloat & ParseInt and perhaps provide some examples of where each should be used.

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

  #2 (permalink)  
Old Jul 29th, 2004, 01:18
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
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.
  #3 (permalink)  
Old Jul 29th, 2004, 09:53
Junior Member
Join Date: Jul 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
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
Closed Thread

Tags
benbacardis, code, logic

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 23:59.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43