By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

array loop problem

100+
P: 132
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
Share this Question
Share on Google+
2 Replies


gits
Expert Mod 5K+
P: 5,333
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

100+
P: 132
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

Post your reply

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