Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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:

__________________
KOR
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
I shared this information with my classmates and we all agree, including our instructor, that it is a wonderful way to be able to add to an existing array later on without having to worry about modifying that part of the code.

The problem is we can't seem to make it work. Are we missing another line of code then what you listed above? Or do we need to list the number in the array the exact number that is in the array. 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++){

My previous post was Problem with form validation. Thank you for your help, it really makes learning these new languages easier when there are generous and kind people like you willing to offer their advice to us newbie coders.
Thanks again.

Casseandra

• I'm sure Kor will be around shortly but if you look at his example:

He is not using any number, he is using the array length which in your previous form validation thread you had 5 items in your array (0-4) but the array length is 5. Therefore: <correctAnswers.length is the same as saying <5 but if you added two more items to the array the length would automagically change with your additions and the new length would be 7 and you would not need to make any further changes to the for loop.

Hope that helps,
I'm not good at 'splainin;
......Willy

• ## 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...

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 ?

• ## 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 <correctAnswers.length> 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
:)

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?

• ## 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.

I am very sorry for the confusion and I sure don't want anyone to think that I am taking advantage of their generosity. I study very hard in every class that I take, probably why I have a perfect 4.0 GPA. So I agree with your statement that all your forum mates and yourself included want everyone who writes to the forums to learn from their mistakes because that always has been the best way to learn, whether its computer languages or cooking.

Thank you for your willingness to help though, it is appreciated very much. I wrote to the forum board for advice the first time because I do not have anyone else that even knows the language to ask when I get stumped on something (including my college instructor, sometimes he doesn't have the answer either). So your advice was a godsend, and I thank you and Willy for that.

Casseandra

• 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;
{
{
score++;
}
}
document.quiz.scorefield.value = score;
}

For future I would suggest u let element used as arrays start at index 0, this makes it easier to code to since then both the array of answers and in this case the array of elements would be 0 indexed meaning the index 0 in both arrays should equal each other.

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

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

• 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

• 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...

• 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++){
score++;
}
}
document.quiz.scorefield.value = score;
}```
.....Willy

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•