array loop problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    array loop problem

    Hi guys,

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

    Code:
    <HTML>
    <HEAD>
    <SCRIPT>
    var lotto = [6, 8, 11, 26, 30, 38];
    var winst = ["2.068.200,00","24.220,00","1.208,00","28,80","2,50",];
    var lotto5 = [3, 8, 16, 18, 28, 38]; 
    
    function check(){
    
    var w5 = new Array();
    
    document.form.output.value += "\n" + "5 : ";
    for (var i in lotto) {
        for (var j in lotto5) {
            if (lotto[i] == lotto5[j]) {
                document.form.output.value += lotto[i] + " ";
                w5.length++;
            }
        }
    }
    
    for (var i in winst) {
    	if (w5.length == winst[i]) {
    		alert(winst[i]);
    	}
    }
    
    }
    
    function execute() {
    check();
    }
    
    </SCRIPT>
    
    </HEAD>
    <BODY>
    
    <form name="form">
    <textarea name="output" cols="40" rows="10"></textarea>
    <br>
    <input type="button" value="Execute" onclick="javascript:execute()">
    <input type="reset" value="reset">
    
    </form>
    </BODY>
    </HTML>
    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
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

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

    btw. in your code you should know that

    [CODE=javascript]w5.length == winst[i][/CODE]
    compares the length (key) to the value (not key) ... so i think you wanted to do the following:

    [CODE=javascript]w5.length == i[/CODE]
    :)

    kind regards

    Comment

    • Cainnech
      New Member
      • Nov 2007
      • 132

      #3
      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

      Comment

      Working...