473,587 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array manipulation question

DL
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.
// 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.

Thanks.
Jun 27 '08 #1
15 1736
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
Jun 27 '08 #2
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?)
Jun 27 '08 #3
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
Jun 27 '08 #4
DL
On May 30, 9:24*am, Erwin Moller
<Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
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
No offense at all, I know it's embrassing and thanks for the book
recommendation.
Jun 27 '08 #5
On May 30, 6:03 am, DL <tatata9...@gma il.comwrote:
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.
// 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.

Thanks.
Here's an interesting way. It occurred to me that the JavaScript
"some" method could be used with a bit of work...

function monoArray(arr) {
function isEqToFirst(ele ment,index,arra y) {
return (element!==arra y[0]);
}
if (arr && arr.length>0) {
return !(arr.some(isEq ToFirst));
}
}

var passed = monoArray([2,5,8,1,4]);
alert(passed); //false
passed = monoArray([12, 12, 12, 12, 12]);
alert(passed); //true
passed=monoArra y([5]);
alert(passed); //true
passed=monoArra y([]);
alert(passed); //undefined
passed=monoArra y();
alert(passed); //undefined
passed=monoArra y(5);
alert(passed); //undefined

This only works for JavaScript interpreters which have the "some"
method. See "compatibil ity" here for the code to add if you're afraid
of running into older, less capable ECMAScript implementations .

http://developer.mozilla.org/en/docs...cts:Array:some
Jun 27 '08 #6
On May 30, 5:50 pm, timothytoe <timothy...@gma il.comwrote:
On May 30, 6:03 am, DL <tatata9...@gma il.comwrote:
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.
// 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.
Thanks.

Here's an interesting way. It occurred to me that the JavaScript
"some" method could be used with a bit of work...

function monoArray(arr) {
function isEqToFirst(ele ment,index,arra y) {
return (element!==arra y[0]);
}
if (arr && arr.length>0) {
return !(arr.some(isEq ToFirst));
}

}

var passed = monoArray([2,5,8,1,4]);
alert(passed); //false
passed = monoArray([12, 12, 12, 12, 12]);
alert(passed); //true
passed=monoArra y([5]);
alert(passed); //true
passed=monoArra y([]);
alert(passed); //undefined
passed=monoArra y();
alert(passed); //undefined
passed=monoArra y(5);
alert(passed); //undefined

This only works for JavaScript interpreters which have the "some"
method. See "compatibil ity" here for the code to add if you're afraid
of running into older, less capable ECMAScript implementations .

http://developer.mozilla.org/en/docs...5_Reference:Gl...
Sent too early. A more compact version of the same thing:

function monoArray(arr) {
if (arr && arr.length>0) {
return !arr.some(funct ion(element,ind ex,array) {
return element!==array[0];
});
}
}
Jun 27 '08 #7
On May 30, 6:03 am, DL <tatata9...@gma il.comwrote:
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.
// 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.

Thanks.
By the way, if the only possibilities for the data are 0 and 1 (as in
your example), you could use Array.reduce to find the sum of all the
elements in the array. If the sum is zero of the length of the array,
you pass. Else you fail.
Jun 27 '08 #8
DL
On May 30, 9:04*pm, timothytoe <timothy...@gma il.comwrote:
On May 30, 6:03 am, DL <tatata9...@gma il.comwrote:


say, we have the following:
// declare anarray
myArray = []; // short hand? *same as myArray = newArray* *?
// populate it
myArray[0] = 0;
myArray[1] = 0;
myArray[2] = 1;
myArray[3] = 0;
// now problem to solve
// fact: the abovearrayhas 4 sets of data, namely 3 zeros and 1 of
value one.
// 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 understandArray has a bunch of methods, but not sure which one is a
good one to solve the above problem.
Thanks.

By the way, if the only possibilities for the data are 0 and 1 (as in
your example), you could useArray.reduce to find the sum of all the
elements in thearray. If the sum is zero of the length of thearray,
you pass. Else you fail.- Hide quoted text -

- Show quoted text -
Wow, the code of your most recent post is elegant. The data is one of
three: 0,1,2. But it looks like one-dimensional array is not good
enough to solve the problem. Too bad my local Barnes and Noble does
not carry the recommended book, can't wait.

Jun 27 '08 #9
On May 30, 6:51 pm, DL <tatata9...@gma il.comwrote:
On May 30, 9:04 pm, timothytoe <timothy...@gma il.comwrote:
On May 30, 6:03 am, DL <tatata9...@gma il.comwrote:
say, we have the following:
// declare anarray
myArray = []; // short hand? same as myArray = newArray ?
// populate it
myArray[0] = 0;
myArray[1] = 0;
myArray[2] = 1;
myArray[3] = 0;
// now problem to solve
// fact: the abovearrayhas 4 sets of data, namely 3 zeros and 1 of
value one.
// 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 understandArray has a bunch of methods, but not sure which one is a
good one to solve the above problem.
Thanks.
By the way, if the only possibilities for the data are 0 and 1 (as in
your example), you could useArray.reduce to find the sum of all the
elements in thearray. If the sum is zero of the length of thearray,
you pass. Else you fail.- Hide quoted text -
- Show quoted text -

Wow, the code of your most recent post is elegant. The data is one of
three: 0,1,2. But it looks like one-dimensional array is not good
enough to solve the problem. Too bad my local Barnes and Noble does
not carry the recommended book, can't wait.
I think it may be overkill for the task at hand, but you seemed
interested in learning how to use the Array methods available in
JavaScript, so I thought I'd give it a shot. Look into Array.map and
Array.reduce as well.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2823
by: Chris Lambacher | last post by:
Hi, I have to do some data manipulation that needs to be fast. I had a generator approach (that was faster than a list approch) which was taking approximately 5 seconds to run both encode and decode. I have done a conversion to pyrex and have gotten it down to about 2 seconds to run both encode and decode. An an excerpt from the pyrex...
6
5627
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading that array back into another file. I've tried, but i can only read the data and place it on the screen, i cant get it into an array. Any help...
3
1393
by: Andrew | last post by:
Hi, How do I convert an array to a string ? What I did was convert a string to an array, after some manipulation, I want to pass the value back to a string. char setchecked; setchecked = strchecked.ToCharArray(); ///// I want to convert array setchecked back to a string n pass the value to
12
2527
by: Sheldon | last post by:
Hi, I have two arrays that are of the same dimension but having 3 different values: 255, 1 or 2. I would like to set all the positions in both arrays having 255 to be equal, i.e., where one array has 255, I set the same elements in the other array to 255 and visa versa. Does anyone know how to do this without using for loops? Sincerely,
12
2632
by: amer.terzic | last post by:
Here's what my code is supposed to do (it seems to work fine) Take a char* array and remove a first character from it....as simple as that. The mentioned char* array is a 'global' var shared among threads. Declared as : char* buffer = (char*) malloc(bufferSize) (later I changed it to use C++ 'new', but it does the same thing)
3
3735
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the string array finally runs out of memory. I'd like to set some line limit and once that limit is reached, start removing the first line each time a new...
1
2280
by: lukemack | last post by:
Hi, I have an array in the following format : Array ( =Array ( =Array (
9
5604
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return String.Format("'{0}'", value) End Function Basically, I just want to surround each value with single quotes. However, I am having trouble...
9
743
by: =?Utf-8?B?R2Vla0JveQ==?= | last post by:
I did not see another group for this so I presume this is the correct one to ask such a question. Anyway, I need to get data entered into a textbox and put it all into an array. I have looked and cannot find such an example of how to accomplish this. What I am trying to do it type in a set of numbers (e.g. 10 20 30....) and fill in an...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.