Solved

I need your help in JavaScript please

Posted on 2007-03-28
8
589 Views
Last Modified: 2010-04-16
I wish to write a script that simulates coin tossing, to let the program toss a simulated coin each time the user clicks the “Flip Coin” button and also allow the user to flip a coin multiple times (or multiple coins!) and further to allow the user to reset the form or forms if he/she use more than one.I will Use a function flip() that takes no arguments and returns false for tails and true for heads

here is my code so far:

<?xml version = "1.0"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
   
    <html xmlns = "http://www.w3.org/1999/xhtml">

<head>


<title>Coin Toss</title>




</head>

<body>
<script Language="JavaScript">
     
     function flip()

</script>
<h1>Coin Toss</h1>

<br><h3>Single Toss</h3></br>
<br><form name="data">
<table border = "0" width="60" cellspacing="0">
<tr>

<td valign = "top"><input type="button" name="click1" value="Flip

Coin"></td>

<td> <input type="text" name="tails-heads"  size="15"></td>
     
 
</tr>
<tr>    

<td><input type="button" name="click2" value="Clear"></td>

</tr>
     
   
</form>

</table></br>

<h3>Multiple Toss</h3>

<form name="dat">
<table border = "1" width="40%">
<tr>
<td>Heads</td>
<td><input type="text" name="heads"  size="15"></td>
</tr>
<tr>
<td>Tails</td>
<td><input type="text" name="tails" size="15"></td>
</tr>
<tr>
<td colspan ="2">
<table>
<tr>
<td>
<input type="button" name"flip" value="Flip Coin">

<input type="text" name="flipcoin" size="9"> Times
</td>
</tr>
<tr>
<td>
<input type="button" name"clear" value="Clear">

</td>
</tr>
</table>
 </td>
</tr>
</form>
</table>
<br>
</body>

</HTML>
0
Comment
Question by:Marylove2007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 18806384
And what is your Question?

0
 

Author Comment

by:Marylove2007
ID: 18806420
the Question how make it work with many thank
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18806446
You make it working by coding scripts.
And your question should be more technically about scripting techniques and less socially like who is willing doing your work and write your application for you and for free ;-)
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 18806469
is this is what you want?

     function flip()
            {
                  if (Math.random() > 0.5))
                        return true;
                  else
                        return false;
            }
0
 

Author Comment

by:Marylove2007
ID: 18807399
wish to write a script that simulates coin tossing, to let the program toss a simulated coin each time the user clicks the “Flip Coin” button and also allow the user to flip a coin multiple times (or multiple coins!) and further to allow the user to reset the form or forms if he/she use more than one.I will Use a function flip() that takes no arguments and returns false for tails and true for heads

here is my code so far:

<?xml version = "1.0"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
   
    <html xmlns = "http://www.w3.org/1999/xhtml">

<head>


<title>Coin Toss</title>




</head>

<body>
<script Language="JavaScript">
     
     function flip()
            {
                  if (Math.random() > 0.5))
                        return true;
                  else
                        return false;
            }

</script>
<h1>Coin Toss</h1>

<br><h3>Single Toss</h3></br>
<br><form name="data">
<table border = "0" width="60" cellspacing="0">
<tr>

<td valign = "top"><input type="button" name="click1" value="Flip

Coin"></td>

<td> <input type="text" name="tails-heads"  size="15"></td>
     
 
</tr>
<tr>    

<td><input type="button" name="click2" value="Clear"></td>

</tr>
     
   
</form>

</table></br>

<h3>Multiple Toss</h3>

<form name="dat">
<table border = "1" width="40%">
<tr>
<td>Heads</td>
<td><input type="text" name="heads"  size="15"></td>
</tr>
<tr>
<td>Tails</td>
<td><input type="text" name="tails" size="15"></td>
</tr>
<tr>
<td colspan ="2">
<table>
<tr>
<td>
<input type="button" name"flip" value="Flip Coin">

<input type="text" name="flipcoin" size="9"> Times
</td>
</tr>
<tr>
<td>
<input type="button" name"clear" value="Clear">

</td>
</tr>
</table>
 </td>
</tr>
</form>
</table>
<br>
</body>

</HTML>
0
 

Author Comment

by:Marylove2007
ID: 18808187
i ALMOST FINISHED THE PROGRAM BUT i HAVE THIS PROBLEM: when i click on 'flip coin' nothing shows up in the text box. I Can't figure out why.

here is my code so far

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML1.0-Transitional//EN"
"http://www.w3.org/TR/xhtml1/dtd/xhtml1-transitional.dtd">

<html>
<head>
<title></title>

<script type ="text/javascript">

var toss, tosstimes, tails, heads;

function singleToss()
{
toss = flip();
if (toss == true)
document.single.singleText.value = "Heads";
else
document.single.singleText.value = "Tails";
}

function multiToss()
{
heads = 0;
tails = 0;
tosstimes = document.multiple.times.value;


for (var count = 0, count < tosstimes, count++)

{
coin = flip();
if (coin == true)
heads = heads + 1;
else
tails = tails + 1;
}

document.multiple.headtext.value = heads;
document.multiple.tailtext.value = tails;
}

function flip()
{
var side;
side = parseInt(Math.random() * 2, 10);
if (side == 1)
return true;
else
false;
}

</script>

<body>

<h2>Lab 07 - Coin Toss</h2>

<h3>Single Toss</h3>

<form name = "single" method ="post" action="">
<input type = "button" value = "Flip Coin" onclick = "singleToss()">
<input type = "text" name = "singleText">
<br>
<input type = "reset" value = "Clear">
</form>

<h3>Multiple Toss</h3>

<form name = "multiple" method ="post" action="">

<table width = "40%" border = "1">
<tr>
<td width="16%">Heads</td>
<td><input type = "text" size = "15" name = "headtext"></td>
</tr>

<tr>
<td width="16%">Tails</td>
<td><input type = "text" size = "15" name = "tailtext"></td>
</tr>

<tr>

<td colspan =2><input type = "button" value = "Flip Coin" onclick = "mutiToss()">


<input type = "text" size = "8" name = "times"> Times <br>
<input type = "reset" value = "Clear"></td>
</tr>

</table>
</form>

</body>
</html>

0
 
LVL 12

Accepted Solution

by:
Sinoj Sebastian earned 250 total points
ID: 18808323
for loop should be "for (var count = 0; count < tosstimes; count++)"
 onclick = "mutiToss()" should be     onclick="multiToss()"

here is the full code:
<html>
      <head>
            <title></title>
            <script type="text/javascript">
                  var toss, tosstimes, tails, heads;
                  function singleToss()
                  {
                        toss = flip();
                        if (toss == true)
                        document.single.singleText.value = "Heads";
                        else
                        document.single.singleText.value = "Tails";
                  }

                  function multiToss()
                  {
                        heads = 0;
                        tails = 0;
                        tosstimes = document.multiple.times.value;
                        for (var count = 0; count < tosstimes; count++)
                        {
                              if (flip() == true)
                              heads = heads + 1;
                              else
                              tails = tails + 1;
                        }
                        document.multiple.headtext.value = heads;
                        document.multiple.tailtext.value = tails;
                  }

                  function flip()
                  {
                        var side;
                        side = parseInt(Math.random() * 2, 10);
                        if (side == 1)
                              return true;
                        else
                              false;
                  }

            </script>
      </head>
            <body>
                  <h2>Lab 07 - Coin Toss</h2>
                  <h3>Single Toss</h3>
                  <form name="single" method="post" action="" ID="Form1">
                        <input type="button" value="Flip Coin" onclick="singleToss()" ID="Button1" NAME="Button1">
                        <input type="text" name="singleText" ID="Text1">
                        <br>
                        <input type="reset" value="Clear" ID="Reset1" NAME="Reset1">
                  </form>
                  <h3>Multiple Toss</h3>
                  <form name="multiple" method="post" action="" ID="Form2">
                        <table width="40%" border="1" ID="Table1">
                              <tr>
                                    <td width="16%">Heads</td>
                                    <td><input type="text" size="15" name="headtext" ID="Text2"></td>
                              </tr>
                              <tr>
                                    <td width="16%">Tails</td>
                                    <td><input type="text" size="15" name="tailtext" ID="Text3"></td>
                              </tr>
                              <tr>
                                    <td colspan="2"><input type="button" value="Flip Coin" onclick="multiToss()" ID="Button2" NAME="Button2">
                                          <input type="text" size="8" name="times" ID="Text4"> Times
                                          <br>
                                          <input type="reset" value="Clear" ID="Reset2" NAME="Reset2"></td>
                              </tr>
                        </table>
                  </form>
            </body>
</html>
0
 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 18808496
why B grade?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question