Re: Array manipulation question



Henry schreef:
On May 30, 2:24 pm, Erwin Moller wrote:
<snip>
var firstVal = myArray[0];

for(var i=0;i<arrLength;i++){
if (myArray[i] != firstVal){
alert ("Black sheep found.");
break;
}}
<snip>

You may as well start the loop counter at 1 as you already know that -
firstVal - will be equal to - myArray[i] - when - i - is zero as you
just set it to that value. (Or was that a deliberate mistake as a
learning exercise?)

Totally true Henry. A few wasted CPU cycles.

A better routine would also check for the number of array elements and stuff like that (eg refuse if only 1 element, or refuse if 0.).

I just wanted the guy to have some material to work with, and didn't give the code too much thought. ;-)

Regards,
Erwin Moller
.