473,414 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Determining the intersection of two arrays.

2
I am having so much trouble with this:(

How would I write a script that compares the integer values in two 5 element arrays.

My script should work like this:

-user enters 5 integers into the first array
-user enters 5 integers into the second array
-script finds integers that the two arrays have in common and stores them in a third array
-script prints the values in the third array

I have to create a function called addCommonValue() that takes an integer and array as arguments, and adds the number to the array
-I also need a global variable that holds the current number of common integers so far, so i can track where the numbers go into the third array.

I need to put all the code in a function(example:begin() that is called with the BODY"onLoad"event.

Can some one plzzzzzzzz help. THank you so much javascript experts
Sep 24 '07 #1
3 8056
pbmods
5,821 Expert 4TB
Heya, Dune. Welcome to TSDN!

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. You have not asked a question. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

MODERATOR
Sep 24 '07 #2
dune1
2
THis is what i have so far...
am i headed in the right direction?
Expand|Select|Wrap|Line Numbers
  1. <script language = "javascript">
  2. var array1 = Array(5, 1, 3, 0); // user entered array #1
  3. var array2 = Array(8, 1, 6, 5); // user entered array #2
  4. var array3 = Array(); // third array
  5.  
  6. array1 = window.prompt ("enter a number","0");
  7. array2 = window.prompt ("enter a number","0");
  8. array3 = window.prompt ("enter a number","0");
  9.  
  10.  
  11.  
  12. for(var i=0;i<array1.length;i++) {
  13. for(var j=0;j<array2.length;j++) {
  14. if(array2[j] == array1[i]) {
  15. array3.push(array2[j]);
  16. }
  17. }
  18. }
  19.  
  20. for(var i=0;i<array3.length;i++) {
  21. document.writeln("Both have number "+ array3[i]);
  22. }
  23. </script>
Sep 24 '07 #3
pbmods
5,821 Expert 4TB
Heya, Dune.

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]

These lines:
Expand|Select|Wrap|Line Numbers
  1. array1 = window.prompt ("enter a number","0");
  2. array2 = window.prompt ("enter a number","0");
  3. array3 = window.prompt ("enter a number","0");
  4.  
are probably what's causing your app not to work, because otherwise, your code looks good. You're replacing array1, array2, etc. with the results of a prompt() which will return a string ("1" / "2" / etc.) instead of an Array.

Also, these lines:
Expand|Select|Wrap|Line Numbers
  1. var array1 = Array(5, 1, 3, 0); // user entered array #1
  2. var array2 = Array(8, 1, 6, 5); // user entered array #2
  3. var array3 = Array(); // third array
  4.  
Really should be:
Expand|Select|Wrap|Line Numbers
  1. var array1 = new Array(5, 1, 3, 0); // user entered array #1
  2. var array2 = new Array(8, 1, 6, 5); // user entered array #2
  3. var array3 = new Array(); // third array
  4.  
Sep 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Antoine Logean | last post by:
Hi, What is the easiest way to get the intersection of two strings in python (a kind a "and" operator) ? ex: string_1 =...
1
by: Bora | last post by:
Should a class be allowed to operate on an array of classes of the same type as itself? Say, class X { ..... void func( X* x); }
4
by: Sebastian Cohen S | last post by:
Hello, I am new to SQL and currently using Access 2003 and need a little help on the following. I have two queries each contains one column formed with a string that is very similar (only...
6
by: kimos | last post by:
hi all, how to calculate the intersection of 2 rectangle a rectangle is the following: Rectangle makeRectangle (Point lowerLeft, Point upperRight) { Rectangle r;
3
by: ryu | last post by:
Hi, May I know how to do an intersection of sets using C#? Where the number of sets will only be known during runtime. Many Thanks
4
by: Jim Andersen | last post by:
Is there anything built into .NET that is good (or rather easy) at comparing ? I have some data (in an array). I make a copy of this array, and the user changes some of the data, or maybe he...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
11
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
1
by: sreeseshu | last post by:
Given are two single-dimensional of int with unique elements. Find the intersection and union of these two arrays. Using C#.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.