Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need your help in JavaScript please

Posted on 2007-03-28
8
Medium Priority
?
611 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
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 
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 750 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

704 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