Solved

I need your help in JavaScript please

Posted on 2007-03-28
8
536 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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 how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now