Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Need help understanding (fairly basic) chunk of code!

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-06-2012, 05:32 AM PM User | #1 Nougati New to the CF scene   Join Date: Nov 2012 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Need help understanding (fairly basic) chunk of code! Hey guys, This is my first time on this website ever, so I have no idea how many people come through here, nor do I know how useful this post will be for me, but bare with me. Anyway, the topic: Without going to far into the redundancies, in my high school IT class, my teacher is a bit... slow. Henceforth I am usually having to accept some parts of my understanding as simply inexistent. This is an example. We started Javascript and began with box sort algorithms (input fields that would sort the values in to an order). However my troubles began when I made it to 10 box sort, and considering my teacher hadn't "come up with the answer yet", troubleshooting my code was virtually bashing my head against a wall. So I got one of my mates from another class to send me his code so I could compare. One thing I noticed is that his code is more comprehensive and has functions that i can only assume his teacher taught him. So if someone could walk me through the process of the program, from the button press to the outcome of the boxes, that would be incredible. I have basic knowledge of all the functions of the loops, but still not extensively. Code: ``` Bubble Sorting

``` Main things I don't understand is: Code: ```var swapped=true ... swapped=false; for (B1=1;B1<=(Max-1);B1++) { if(myNums[B1]>myNums[B1+1]) { temp=myNums[B1]; myNums[B1]=myNums[B1+1]; myNums[B1+1]=temp; swapped=true; }``` and Code: ```var count; ... for(count=1; count<=Max;count++) { myNums[count]=parseInt(document.getElementById('box'+count).value); }``` Any help whatsoever would be amazing. Thanks.
 11-06-2012, 08:33 AM PM User | #2 devnull69 Senior Coder   Join Date: Dec 2010 Posts: 2,353 Thanks: 11 Thanked 557 Times in 550 Posts The first code part describes the swapping of two variable values. In order to swap two variable values you need a third (temporary) variable storing one of the values before you overwrite it with the other, because as soon as you copy one variable to the other, both variables will have the same value and the other one would be lost. The second code part is a simple for loop counting from 1 to Max (in the variable count). Then it puts the values/content of the boxes with id "box1", "box2" .... "box" into the array myNums. parseInt makes sure that the content will be converted into a numeric value (if possible, otherwise it will be NaN). Last edited by devnull69; 11-06-2012 at 08:56 AM..
11-06-2012, 08:40 AM   PM User | #3
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,473
Thanks: 200
Thanked 2,469 Times in 2,447 Posts
Quote:
 Originally Posted by devnull69 parseInt makes sure that the content will be converted into a numeric value (if possible, otherwise it will be NaN).
If you use parseInt() you should also specify the radix (10).

Better to use Number() to convert a string value to a number. You can trap NaN with

myNums[count]=Number(document.getElementById('box'+count).value) || 0; // set value to 0 if NaN
__________________

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.

Last edited by Philip M; 11-06-2012 at 08:42 AM..

 11-06-2012, 04:15 PM PM User | #4 xelawho Senior Coder     Join Date: Nov 2010 Posts: 2,590 Thanks: 54 Thanked 476 Times in 474 Posts This may be getting ahead of where you are, but if your teacher is useless you may as well start learning for yourself: you can make your code more dynamic by simply specifying which group of inputs you want to get the values from. In the code below this is done by putting them all into a div and then collecting them by tag name. Note that this also does away with the need for giving your inputs id's or names. The other thing that may be of interest is that javascript has an inbuilt sort method for arrays, so all you really need to do is collect the values from the boxes, push them onto an array, sort the array and then repopulate the boxes with the newly sorted array values. Have a look: Code: ``` Bubble Sorting

``` Last edited by xelawho; 11-06-2012 at 05:03 PM.. Reason: added info

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:37 PM.