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 Question for Kor on his previous advice on arrays

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 04-23-2004, 11:56 AM PM User | #1 Casseandra New Coder   Join Date: Apr 2004 Posts: 14 Thanks: 0 Thanked 0 Times in 0 Posts Question for Kor on his previous advice on arrays Kor you gave me some advice on arrays on a previous post that I had made. This is advice that you gave me. As Willy says, the counting in a loop statement starts from 0. It should stop when all the array's elements were seen. That means the stop should be less than the last arryay's element order+1, which is equal with the array's length, 5 your case for (i=0; i<5; i++){ In practice is a better ideea to use directly the length attribute, to avoid further confusion or unecessary modifies when the array is modified later.Your case: for (i=0; i
 04-23-2004, 12:31 PM PM User | #2 Willy Duitt Banned   Join Date: Sep 2003 Posts: 3,620 Thanks: 0 Thanked 0 Times in 0 Posts I'm sure Kor will be around shortly but if you look at his example: for (i=0; i
 04-23-2004, 01:13 PM PM User | #3 Kor Red Devil Mod     Join Date: Apr 2003 Location: Bucharest, ROMANIA Posts: 8,478 Thanks: 58 Thanked 379 Times in 375 Posts here I am Array has two different, let's say, proprieties: order of each element and total number of elemets. The array's order couting starts from 0 (as it is in Javascript, don't ask me why, it is not my language ). The total number counting of elements starts, as in "normal" math way, from 1. This is the reason why always the last element's order value is lower with an unit than the total number. Total number can be assign dynamically as the javascript implemented attribute called length. The syntax is object.length, in your case the object is the variable declared as array. Now let's see the loop. The incremental variable, i, counts the order, NOT the number, that means it must start from 0 and stop when the last counted element was seen. This stop value can be an integer, but the best way is to assign it dynamically as the array's length. Thus you may later add or substract arrays elements without modify the loop's code line... Was I enough clear... __________________ KOR Offshore programming -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
04-23-2004, 01:22 PM   PM User | #4
Kor
Red Devil Mod

Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts

Quote:
 For example, 5 items in the array, would we code the line for (i=0; i<5; i++){ or for (i=0; i<6; i++){
Following my former explanation
for (i=0; i<5; i++){
There are 5 items in array, but the last element in array has, as order number, 4 (0,1,2,3,4 make 5 items, yes? ). As i counts the order, the last i must be, thus, 4, that means it must be less than 5, correct?

But if using
instead of an integer, you don't need to bother about the order, the number and stuf like that.

correctAnswers.length will always substitute the correct incremental stop, nomatter you add or substitute elements. 5 or 546324 elements, the length will do the job. Why counting when you can leave the computer to do that for you ?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

 04-23-2004, 05:22 PM PM User | #5 Casseandra New Coder   Join Date: Apr 2004 Posts: 14 Thanks: 0 Thanked 0 Times in 0 Posts Thank you to both Willy and Kor Thank you both for the explanations. I have JavaScript class in a couple hours and I printed out your explanations to share with my classmates. We will try it again the in our code, we must of had some other coding errors when we tried it the first time because we could not get it to work. Oh the joys of coding! :p Again, I want to thank you both for helping me, it is very much appreciated. Big hugs to you both! Casseandra :)
 04-23-2004, 06:57 PM PM User | #6 Kor Red Devil Mod     Join Date: Apr 2003 Location: Bucharest, ROMANIA Posts: 8,478 Thanks: 58 Thanked 379 Times in 375 Posts Ok. We will be able, i think , to help you and your classmates.. We (all the forum's mate, i dare to say) might be able to correct your codes but, (important to us, I dare again to say) the most important think is trying to teach you learning from your own mistakes... *Better teach somene how to handle himself a fishing rod, than to give him a fish* Good saying.... hm... So... which is your problem now? __________________ KOR Offshore programming -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 04-24-2004, 10:37 AM PM User | #7 Casseandra New Coder   Join Date: Apr 2004 Posts: 14 Thanks: 0 Thanked 0 Times in 0 Posts My apologies, I didn't make myself clear enough I think. ::blushes:: Oh, you misunderstood me, my apologies. My original assignment worked perfectly after Willy told me that I was missing a -1 in my code and I needed to remove the reference to the external library. Those were my only problems and I never would have figured out on my own about the [i -1] without his help and of course yours Kor. I do prefer to debug and write my own code because I really want to learn all the different computer languages and be able to eventually stand on my own two feet without having to ask for assistance with any problems. :o). I normally only ask for help or advice when I've tried out all the knowledge that I have in my head on the language I'm learning at the time, and I cannot find the answer in my textbook, or anywhere else I've looked. If you could see all the computer language books I've boughten in the last few years you would probably split a seam from laughing at me. Anyways, After I and the rest of my classmates turned in our assignments, we took our codes and inserted the i
 04-24-2004, 07:06 PM PM User | #8 Garadon Regular Coder   Join Date: Jul 2002 Posts: 698 Thanks: 0 Thanked 0 Times in 0 Posts The problem was that. 1. if you used willy and kor suggestion you end up asking for negative indexes in the array, should make a noticable yellow error flag in bottom left of ie. 2. if you only used kor suggestion you would end u searching for question 0. the working kode would be: function ca() { var score = 0; for (i=0; i
 04-25-2004, 01:27 AM PM User | #9 Casseandra New Coder   Join Date: Apr 2004 Posts: 14 Thanks: 0 Thanked 0 Times in 0 Posts Thank you Garadon! Thank you for your advice, I made a note of your suggestions in my JavaScript Tips Binder so I won't forget. That way I don't make the same mistakes twice. Casseandra
 04-25-2004, 03:15 AM PM User | #10 Willy Duitt Banned   Join Date: Sep 2003 Posts: 3,620 Thanks: 0 Thanked 0 Times in 0 Posts Garadon; Although I whole heartedly agree that you should begin your count at zero, I do not agree with your assumption that a javascript error would be thrown by any negative indexes in the array if beginning your (var i) count at one and using [i-1] at worse, the index would be zero, (1-1) and there never will be an instance of negative value. .....Willy
 04-25-2004, 08:38 AM PM User | #11 Garadon Regular Coder   Join Date: Jul 2002 Posts: 698 Thanks: 0 Thanked 0 Times in 0 Posts Your rigth and I never claimed it would I said if you used urs and kors together that would happen which is true since kor started at 0 and 0-1=-1
 04-26-2004, 09:01 AM PM User | #12 Kor Red Devil Mod     Join Date: Apr 2003 Location: Bucharest, ROMANIA Posts: 8,478 Thanks: 58 Thanked 379 Times in 375 Posts Do I missed smth? I don't get the point with i-1 and negative values... The way the code is, there is no need to use negatives... __________________ KOR Offshore programming -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 04-26-2004, 10:42 AM PM User | #13 Willy Duitt Banned   Join Date: Sep 2003 Posts: 3,620 Thanks: 0 Thanked 0 Times in 0 Posts Nothing to it really. But it goes back to this thread and the i counting from one while an array counts from 0 and instead of rewriting anything I merely pointed out the script would work as written if the correctAnswers had 1 subtracted from i. .....Willy Code: ```function checkAnswers(){ var score = 0; for (i=1; i<6; i++){ answers = getTextValue("quiz","q" + i); if (answers == correctAnswers[i-1]){ score++; } } document.quiz.scorefield.value = score; }``` .....Willy

 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 12:42 PM.