Re: Array manipulation question
- From: Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@xxxxxxxxxxxxxxxx>
- Date: Fri, 30 May 2008 15:24:51 +0200
DL schreef:
say, we have the following:
// declare an array
myArray = []; // short hand? same as myArray = new Array ?
// populate it
myArray[0] = 0;
myArray[1] = 0;
myArray[2] = 1;
myArray[3] = 0;
// now problem to solve
// fact: the above array has 4 sets of data, namely 3 zeros and 1 of
value one.
Hi,
Well, they do not contain a 'set' but just a plain value.
// say, the business rule is, if all set of data are of same value ( 0
|| 1), then do nothing
else alert ('hey dark sheep found').
I understand Array has a bunch of methods, but not sure which one is a
good one to solve the above problem.
No offense, but this is so basic I advise you to buy a book on JavaScript (any will do, but O'Reilly's Definitive Guide is good).
Here follows an example to get you going.
(It can be written in many different ways, and shorter, but I hope this version is clear to you.)
<script type="text/javascript">
var myArray = [];
// populate it
myArray[0] = 0;
myArray[1] = 1;
myArray[2] = 0;
myArray[3] = 0;
var arrLength = myArray.length;
var firstVal = myArray[0];
for(var i=0;i<arrLength;i++){
if (myArray[i] != firstVal){
alert ("Black sheep found.");
break;
}
}
</script>
Thanks.
You're welcome.
Regards,
Erwin Moller
.
- Follow-Ups:
- Re: Array manipulation question
- From: DL
- Re: Array manipulation question
- From: Henry
- Re: Array manipulation question
- References:
- Array manipulation question
- From: DL
- Array manipulation question
- Prev by Date: Re: Turn of the "Ding" sound when user presses enter
- Next by Date: Re: Array manipulation question
- Previous by thread: Array manipulation question
- Next by thread: Re: Array manipulation question
- Index(es):
Relevant Pages
|