Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    How would I do this

    So I want to make a little javascript that when you enter the name of two combatants, it gives you the one with the higher ranking as the winner.

    For example

    combatant 1 = 7000
    combatant 2 = 9000

    prompt("Who is the first combatant?")

    prompt("Who is the second combatant?")

    alert(//a way to make the combatant with the higher rank win)

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Well, to start with, you would not use prompt() or alert().

    Both are considered obsolete and should not be used with modern browsers.

    If your instructor is telling you to use them, then your instructor is at least 6 or 7 years behind the times.

    In any case, this appears to be homework, and we don't *DO* homework. See RULE 1.5 in http://www.codingforums.com/rules.htm

    *IF* you make some reasonable attempt at doing your own homework and just need a little help here and there, *THEN* you will almost surely get it. The more work you do, the more likely you are to get *GOOD* help.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Well, to start with, you would not use prompt() or alert().

    Both are considered obsolete and should not be used with modern browsers.

    If your instructor is telling you to use them, then your instructor is at least 6 or 7 years behind the times.

    In any case, this appears to be homework, and we don't *DO* homework. See RULE 1.5 in http://www.codingforums.com/rules.htm

    *IF* you make some reasonable attempt at doing your own homework and just need a little help here and there, *THEN* you will almost surely get it. The more work you do, the more likely you are to get *GOOD* help.
    This is not homework, I am not in sort of programming class considering im not yet out of high school.

    This is something I want to do to help rank anime fighters (I intend to expand so that things like location play a role), and all the knowledge I ahve comes from the Codeacademy tutorial.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Look at the other threads you started:
    How to get word variables[CODE]

    Javascript submit not working? [CODE]

    You should use the same kind of code. A <form> with <input>s, etc.

    And especially in that second thread you can see how to match a "key" against a set of "key : value" pairs.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Example of what I mean about key value pairs:

    Code:
    var combatants = {
        "Mighty Joe" : 7000,
        "Hairy Moe" : 9000,
        "Wimpy Woe" : 100
    };
    You see? The user enters a name (or, better, picks it from a <select> list) and you can find the number of points associated with that name by just doing:
    Code:
        var points = combatants[ nameUserEntered ];
        if ( points == null )
        {
            ... user did not enter a valid name ...
        }
    Does that get you started?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hossaim (02-01-2013)

  • #6
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Example of what I mean about key value pairs:

    Code:
    var combatants = {
        "Mighty Joe" : 7000,
        "Hairy Moe" : 9000,
        "Wimpy Woe" : 100
    };
    You see? The user enters a name (or, better, picks it from a <select> list) and you can find the number of points associated with that name by just doing:
    Code:
        var points = combatants[ nameUserEntered ];
        if ( points == null )
        {
            ... user did not enter a valid name ...
        }
    Does that get you started?
    Yes it did.

    Thanks for the help!

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Actually, if you use <select>s, you don't need the key/value pairs.
    Code:
    <select name="combatant1">
       <option value="0">--choose--</option>
       <option value="9000">Mighty Joe</option>
       <option value="7000">Hairy Moe</option>
       <option value="100">Wimpy Woe</option>
    </select>
    SO the same for the other combatant. Get the .value of each <select> and presto! You have the points.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You can also get the name that was picked from a <select>, thus:
    Code:
        var form = document.getElementById("idOfYourForm");
        var sel = form.combatant1;
        var fighter1Name = sel.options[sel.selectedOption].text;
        var fighter1Points = Number(sel.value);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Actually, if you use <select>s, you don't need the key/value pairs.
    Code:
    <select name="combatant1">
       <option value="0">--choose--</option>
       <option value="9000">Mighty Joe</option>
       <option value="7000">Hairy Moe</option>
       <option value="100">Wimpy Woe</option>
    </select>
    SO the same for the other combatant. Get the .value of each <select> and presto! You have the points.
    Ok.

    The only problem im having is finding a way to call this so when a user puts in combatant 1 and combatant 2, it gives him a message saying combatant 2 wins. I can't figure out how to do it in the submit box from my last code.

    The function that you gave me the last time:
    function dotranslate( btn )
    {
    var word = btn.form.translateFrom.value;
    btn.form.translateTo.value = dictionary[ word.toLowerCase() ];
    }

    is very confusing and I can't seem to work it for this one, and get a second box to work.
    Last edited by hossaim; 02-01-2013 at 06:50 AM.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Which version are you using? The key/value map? Or the <select>s?

    If it's the selects:
    Code:
    <form id="MyForm">
    First fighter: <select name="combatant1">
       <option value="0">--choose--</option>
       <option value="9000">Mighty Joe</option>
       <option value="7000">Hairy Moe</option>
       <option value="100">Wimpy Woe</option>
    </select><br/>
    Second fighter: <select name="combatant2">
       <option value="0">--choose--</option>
       <option value="9000">Mighty Joe</option>
       <option value="7000">Hairy Moe</option>
       <option value="100">Wimpy Woe</option>
    </select><br/>
    <input type="button" value="Check for winner" onclick="check(this.form);"/>
    </form>
    <div id="message"></div>
    
    <script type="text/javascript">
    function check(form)
    {
        var c1 = form.combatant1.value;
        var c2 = form.combatant2.value;
        var msg;
        if ( c1 > c2 ) { msg = "Fighter 1 wins"; }
        else if ( c1 < c2 ) { msg = "Figher 2 wins"; }
        else { msg = "You chose the same fighter both times!  Cheater!"; }
        
        document.getElementById("message").innerHTML = msg;
    }
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hossaim (02-06-2013)

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    By the way, that code does *NOT* check to make sure the user has made *SOME* choice for both fighters.

    Can you figure out how to check for that? And what to do about it?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hossaim (02-06-2013)


  •  

    Posting Permissions

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