Solved

I need your help in JavaScript please

Posted on 2007-03-28
8
575 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
  • 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to read data from SQL server  to sharepoint 2013 ? 3 29
Html input text color the frame line 2 22
Copying table data to one another 15 31
2 separate CSS animations 2 17
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 …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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…

821 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