Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 03:49 AM   PM User | #1
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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)
hossaim is offline   Reply With Quote
Old 02-01-2013, 06:28 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,212
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 02-01-2013, 06:30 AM   PM User | #3
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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.
hossaim is offline   Reply With Quote
Old 02-01-2013, 06:32 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,212
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Look at the other threads you started:
http://www.codingforums.com/showthread.php?t=283612

http://www.codingforums.com/showthread.php?t=283667

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.
Old Pedant is online now   Reply With Quote
Old 02-01-2013, 06:36 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,212
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
hossaim (02-01-2013)
Old 02-01-2013, 06:39 AM   PM User | #6
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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!
hossaim is offline   Reply With Quote
Old 02-01-2013, 06:39 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,212
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 02-01-2013, 06:41 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,212
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 02-01-2013, 06:45 AM   PM User | #9
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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..
hossaim is offline   Reply With Quote
Old 02-01-2013, 07:33 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,212
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
hossaim (02-06-2013)
Old 02-01-2013, 07:35 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,212
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
hossaim (02-06-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:26 PM.


Advertisement
Log in to turn off these ads.