[RESOLVED] Compare three values
Results 1 to 8 of 8

Thread: [RESOLVED] Compare three values

  1. #1
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    461

    resolved [RESOLVED] Compare three values

    I'm trying to compare three values randomly generated, but I can't quite seem to get it, I've tried every combination I can think of


    PHP Code:
    $(document).ready(function() {
     
      $( 
    "#click" ).click(function() {
        var 
    image1 = new Array ();
        var 
    number1 Math.floor(Math.random() * 3);
        
    image1[0] = "<img src='images/ace.jpg'>";
        
    image1[1] = "<img src='images/king.png'>";
        
    image1[2] = "<img src='images/queen.png'>";

        var 
    image2 = new Array ();
        var 
    number2 Math.floor(Math.random() * 3);
        
    image2[0] = "<img src='images/ace.jpg'>";
        
    image2[1] = "<img src='images/king.png'>";
        
    image2[2] = "<img src='images/queen.png'>";

        var 
    image3 = new Array ();
        var 
    number3 Math.floor(Math.random() * 3);
        
    image3[0] = "<img src='images/ace.jpg'>";
        
    image3[1] = "<img src='images/king.png'>";
        
    image3[2] = "<img src='images/queen.png'>";

        $(
    '.one').html(image1[number1]);
        $(
    '.two').html(image2[number2]);
        $(
    '.three').html(image3[number3]);

        if(
    number1 && number2 && number3 === number1 && number2 && number3){
          
    console.log("you win");
           $(
    '.win').html("<h1>You win!!</h1>");
        }
        else{
          $(
    '.win').html("<h1>Try again!!</h1>");
          
    console.log("You lose");
        }

      });
    }); 
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,505
    Code:
    if(number1 && number2 && number3 === number1 && number2 && number3)
    (I wonder why I put this here).

    (I wonder what I meant).

    (Perhaps I was looking for an association?)
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    461
    PHP Code:
    $( "#click" ).click(function() {
        var 
    image1 = new Array ();
        var 
    number1 Math.floor(Math.random() * 3);
        var 
    one = $(".one"),
        
    two = $(".two"),
        
    three = $(".three");
        
    image1[0] = "<img src='images/ace.jpg'>";
        
    image1[1] = "<img src='images/king.png'>";
        
    image1[2] = "<img src='images/queen.png'>";

        $(
    one).html(image1[number1]);
        $(
    two).html(image1[number1]);
        $(
    three).html(image1[number1]);

        if(
    one && two && three === one && two && three){
          
    console.log("you win");
           $(
    '.win').html("<h1>You win!!</h1>");
        }
        else{
          $(
    '.win').html("<h1>Try again!!</h1>");
          
    console.log("You lose");
        } 
    Well this maybe a little less ugly, although maybe "more" wrong but back to values and searching online values and associations

    nope
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,518
    Depending on what it is you're trying to compare (objects only compare identical to themselves), a, b, and c are identical if a === b && b === c. (Contrariwise, if a and b are different objects, then a !== b even if they look identical, with identical properties.)
    Last edited by Weedpacket; 08-30-2017 at 07:12 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Junior Member
    Join Date
    Jul 2017
    Location
    Coimbatore, India
    Posts
    2
    Hi cluelessPHP,
    Since you are using the same set of images, it is enough if you compare number1, number2 and number 3. As weedpacket mentioned above, use if((number1==number2) && (number2==number3))

  6. #6
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    461
    Oh I didn't know you could do it like that
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  7. #7
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    461
    Oh bugger I marked it as resolved and it doesn't work
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  8. #8
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    461
    PHP Code:
     var number1 Math.floor(Math.random() * 3); 
    Like that and it works, thanks all
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •