Solved

I need your help in JavaScript please

Posted on 2007-03-28
8
564 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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)

772 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