0

I'm using jQuery, Hello, I'm making some "Lotto" type of game, where user is giving numbers, and it checks has he matched any of randomly generated numbers from 1-30. Problem is in last part, when im using "if" to check are numbers generated and added by user the same. I checked is the code taking good numbers to compare, and it worked good, but for some reason "if" statement never gave me positive result, even if the numbers matched. What should i do to make it work? *edit The problem is with THE LAST if "if(wylosowaneLiczby[a]===wybraneLiczby[b])"

$(document).ready(function(){
    var wybraneLiczby = [];
    var wylosowaneLiczby = [];
    var i=1;
    var a=0;
    var b=0;

    var licznik = 0;
    $("a#dodaj").click(function(){
        var zmienna = $("input#wybranaLiczba").val();
        if(isNaN(zmienna)===true || zmienna===""){
            alert("Podaj liczbe");
        }
        else 
        {
            wybraneLiczby.push(zmienna);
            $("p#wybraneLiczby").append(zmienna).append(", ");
            $("input#wybranaLiczba").val("").focus();

            if(wybraneLiczby.length ===7)
            {
                $("div#losowanie").slideUp("slow");
                $("#losuj").delay(1000).slideDown("slow");
            }
        }

    })
    $("a#losuj").click(function(){
        var zmienna = Math.round(Math.random()*10+Math.random()*10+Math.random()*10);
        wylosowaneLiczby.push(zmienna);
        licznik+=1;
        $("p#wylosowane").append(zmienna).append(", ");
        if(licznik===7)
        {
            $("#losuj").slideUp("fast");
            for(i=0;i<7;i++)
            {
                for(a=0;a<7;a++){
                    if(wylosowaneLiczby[a]===wybraneLiczby[b])
                    {
                        $("p#wygrane").append(wylosowaneLiczby).append(", ");
                    }
                }
            b++;
            }


        }
    })


})
Marcin Szaro
  • 49
  • 1
  • 7
  • 2
    Which part exactly? I assure you, If-statements work fine. – Carcigenicate Dec 26 '17 at 12:34
  • The last: if(wylosowaneLiczby[a]===wybraneLiczby[b]). Like, in my page it never gave positive. I tried to make it alert("works") if they match, but the popup never worked, same as adding the number to paragraph. – Marcin Szaro Dec 26 '17 at 12:35
  • The last one. if(wylosowaneLiczby[a]===wybraneLiczby[b]) – Marcin Szaro Dec 26 '17 at 12:37
  • 1
    You are using strict comparison operator. make sure the dataTypes and values has to match. – Kumar_Vikas Dec 26 '17 at 12:38
  • So debug it checking value you get compare to value you expect to get. This is really hard to debug without any sample replicating your issue and especially with so unreadable variable naming... – A. Wolff Dec 26 '17 at 12:39
  • @MarcinSzaro I'm going to guess that the elements of one are strings, while the elements of the other are numbers. If you do some debugging and that's the case you need to either turn the numbers into strings, or parse the strings as numbers. They must be the same type to compare them like that. – Carcigenicate Dec 26 '17 at 12:39
  • Ya, it looks like `zmienna` is a string... – A. Wolff Dec 26 '17 at 12:41
  • Thank you very much. It worked. I deleted 1 "=" in last and it worked. – Marcin Szaro Dec 26 '17 at 12:41

1 Answers1

0

=== is basically used for strict comparison. For strict equality, the objects being compared must have the same type. you can see the answer like, Difference between == and === in JavaScript #use == rather than === #

MANITORATION
  • 557
  • 2
  • 5
  • 19