473,385 Members | 1,856 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,385 software developers and data experts.

array loop problem

132 100+
Hi guys,

It's me again. Arrays are a mindbuster for me. I'm stuck again but I can't see the error.

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <SCRIPT>
  4. var lotto = [6, 8, 11, 26, 30, 38];
  5. var winst = ["2.068.200,00","24.220,00","1.208,00","28,80","2,50",];
  6. var lotto5 = [3, 8, 16, 18, 28, 38]; 
  7.  
  8. function check(){
  9.  
  10. var w5 = new Array();
  11.  
  12. document.form.output.value += "\n" + "5 : ";
  13. for (var i in lotto) {
  14.     for (var j in lotto5) {
  15.         if (lotto[i] == lotto5[j]) {
  16.             document.form.output.value += lotto[i] + " ";
  17.             w5.length++;
  18.         }
  19.     }
  20. }
  21.  
  22. for (var i in winst) {
  23.     if (w5.length == winst[i]) {
  24.         alert(winst[i]);
  25.     }
  26. }
  27.  
  28. }
  29.  
  30. function execute() {
  31. check();
  32. }
  33.  
  34. </SCRIPT>
  35.  
  36. </HEAD>
  37. <BODY>
  38.  
  39. <form name="form">
  40. <textarea name="output" cols="40" rows="10"></textarea>
  41. <br>
  42. <input type="button" value="Execute" onclick="javascript:execute()">
  43. <input type="reset" value="reset">
  44.  
  45. </form>
  46. </BODY>
  47. </HTML>
  48.  
I've skimmed out the script for this post just to include the troubled part.

These are two compare functions which compare arrays. The first one works perfectly. It shows the common numbers in the textarea.

The second one is the problem.
What it should do is get the total amount of matching numbers and then show the value which corresponds with it in the array "winst".

So if w5.length is 2 then the alert should display winst[2]

But somewhere inside the script I've messed up.

If anyone sees it, I would appreciate it if you could point it out.

thanks,

Cainnech
Jan 6 '08 #1
2 1249
gits
5,390 Expert Mod 4TB
hi ...

have a look here ... and try to adapt it to your needs ...

btw. in your code you should know that

Expand|Select|Wrap|Line Numbers
  1. w5.length == winst[i]
compares the length (key) to the value (not key) ... so i think you wanted to do the following:

Expand|Select|Wrap|Line Numbers
  1. w5.length == i
:)

kind regards
Jan 6 '08 #2
Cainnech
132 100+
Thanks for the tip André. I found that post a few days ago and I copied that to use in my script.

And in fact, what I'm trying to do by using the w5.length is to get the key and then look for the corresponding key in the array "winst"and return the value of that key.

So if the key of w5.length is 2, I want the script to look for the second key of the array "winst" and return that value in to the alert.

But your explanation did let me realize that I was indeed wrong in that equasion. So I need to put the keys in the equasion and then get the value out of it.

Greets,

Kenneth
Jan 6 '08 #3

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

Similar topics

3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
8
by: Niron kag | last post by:
Hi, I have a loop an in the loop I populate an array of dataRow. My problem is the array keeps its values from previous loop, and its Length gets bigger every loop. My code is: DataRow...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
3
by: assgar | last post by:
Hi I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2) a display function on the HTML form and 3) a...
4
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...

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.