Tossing a coin

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1080 Views - Last Post: 17 February 2013 - 10:19 AM

#1 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Tossing a coin

Posted 14 February 2013 - 01:19 PM

So I am trying to design a bit of Javascript that will allow me to toss a coin what I have so far is posted below. I have gotten the code to display a simple button but now I am not sure how to display the results that I am after. I am supposed to toss a coin and in the function flip I am only supposed to return true or false but I am not sure how to really manipulate this function so that it will produce any results. Any light to right my path in this dark maze is appreciated.

<!DOCTYPE html>
	<html>
		<head> 
			<meta charset ="utf-8">
			<script>
			
				function load()
					{
						var button = document.getElementById("tossButton");
						button.addEventListener("click", tossCoin, false);
						if(flip()==true)
							document.write("heads");
						else
							document.write("tails");
					}	
				
				function flip()
					{
						if(Math.floor(Math.random()*2)==1)
							return true; 
						else 
							return false;
					}

				}
			
			</script>
		</head>
		<body>
			<form action ="#">
				<input id ="tossButton" type = "button" value="Toss Coin">
			</form>
		</body>
	</html>  
	



Is This A Good Question/Topic? 0
  • +

Replies To: Tossing a coin

#2 runfaster  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 135
  • Joined: 23-January 09

Re: Tossing a coin

Posted 14 February 2013 - 02:24 PM

Here's a relevant link to a description of the method: W3 School Page

I would look at that, and look at your if statement in flip(). Your comparison is your problem.

Pay special attention to the range of the random method and the fact that you want a given result half of the time.

That's all I've got for you at a glance. For displaying results, however, someone else will have to help you. Unfortunately, I'm not that familiar with javascript.

This post has been edited by runfaster: 14 February 2013 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,023
  • Joined: 12-December 12

Re: Tossing a coin

Posted 14 February 2013 - 02:35 PM

You have a syntax error, an extra closing bracket }.
You aren't calling your functions at any point, as I'm sure you are aware.

However, IMO, you should be reading a tutorial, rather than posting a question for this; there are tutorials available here at DIC - see the Tutorials link at the top of this page.
Was This Post Helpful? 0
  • +
  • -

#4 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Tossing a coin

Posted 14 February 2013 - 02:54 PM

Okay so I found and deleted the extra } but its still not rendering. What do you mean that I am not calling my function? Am I not calling it with the statement if(flip()==true)? Do you know of any good IDE's with error detection for javascript like netbeans or eclipse has for java? Sorry I am a complete noob at web development.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,023
  • Joined: 12-December 12

Re: Tossing a coin

Posted 14 February 2013 - 03:02 PM

I use Komodo Edit which highlights errors in lots of languages.

button.addEventListener("click", tossCoin, false);

This adds a function called tossCoin to the click event for your button, but you have no function of this name.

BTW I can't see anything wrong with your use of random().

And you are attaching the click event within your load() function, which is never called.

Posted Image

This post has been edited by andrewsw: 14 February 2013 - 03:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: Tossing a coin

Posted 14 February 2013 - 03:03 PM

One problem that you might be having is the placement of your <script> tag in the page. when the page loads the Javascript is going to run before your document loads. That means when you try to find the button in your load method it's not going to find anything because the DOM has not loaded. For this example move your scripts to after the closing html tag (bottom of the page)
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Tossing a coin

Posted 15 February 2013 - 01:41 AM

Well, here are a few observations and suggestion that I would like to make:

1. As andrewsw has already mentioned, there is an extra } in your line 23

2. The button click method needs to be calling a method, preferably the load() method, so I would suggest you give a function call like this in your input tag for button directly.

<input id ="tossButton" type = "button" value="Toss Coin" onclick="load()">


3. Once You have done that, the lines 9 and 10 of your program become redundant...would be wise to comment them out.

4. In this load method, you need to call your flip() method.

5. Your random number should not be called like that...instead take a look at this page as to how to get a value between 1 and 10.

6. Now, consider head as 0 and tail as 1. Make the if statement in such a way that if the value returned is 1, then heads, else tails.

I will give you a small hint. You can probably use the % operator for it.

Alright, I believe with these many hints, you should be able to make your program run without any difficulties.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#8 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Tossing a coin

Posted 15 February 2013 - 11:08 AM

Thank you to everyone for their suggestions so far... they are pointing me in the right direction of what I want to accomplish. To further my goal of where I want to be I need some knowledge laid on me of how to accomplish a simple display of results once a button has been clicked and how to redisplay my buttons and keep a running set of results on my previous clicks.
<!DOCTYPE html>
<html>
    <head> 
            <meta charset ="utf-8">
            <script>
            var head = 0;
            var tails = 0;
            var totalFlips=0;
                    function load()
                            {
                                if(flip()==true)
                                    ++head;
                                else
                                    ++tails;
                            ++totalFlips;
                            document.writeln("heads: "+head);
                            document.writeln("tails: "+tails);
                            }	
                    
                    function flip()
                            {
                                if(Math.floor(Math.random()*2)==1)
                                    return true; 
                                else 
                                    return false;
                            }
                    function stop()
                        {
                            document.writeln("You chose not to toss the coin... Have a good day!");
                        }
                   
            </script>
    </head>
    <body>
            <form action ="#">
                    <p>Click Toss Coin if you would like to toss the coin, otherwise click exit.</p>
                    <input id ="tossButton" type = "button" value="Toss Coin" onclick="load()">
                    <input id =quitButton" type = "button" value="Exit" onclick="stop()">
                    
            </form>
    </body>
</html>  


Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,023
  • Joined: 12-December 12

Re: Tossing a coin

Posted 15 February 2013 - 11:27 AM

You have another syntax error here:

<input id =quitButton" type = "button"

..it seems that you didn't take my advice and install Komodo Edit, or similar, as this error would have been made clear.

BTW Two people have now suggested your use of random() is incorrect; well, unless, I'm missing something(?) it isn't :whistling:

View PostSnow1986, on 15 February 2013 - 11:08 AM, said:

I need some knowledge laid on me

You might rephrase this as I need to do some more studying to get where I want to be.

This post has been edited by andrewsw: 15 February 2013 - 11:29 AM

Was This Post Helpful? 1
  • +
  • -

#10 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Tossing a coin

Posted 15 February 2013 - 11:35 AM

I am using Komodo edit so apparently and it didn't catch it but it also didn't cause any issues in running the script. I have done some studying on the use of my random method and it keeps pointing me in that direction so I will continue to use it that way not to mention I isolated it and tested it and it is producing the results I am needing. I have done some studying and I can't figure it out that's why I came here. I always try to do things on my own before I try seeking the advice of others.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,023
  • Joined: 12-December 12

Re: Tossing a coin

Posted 15 February 2013 - 11:46 AM

You must be using it in a different way to the way I am :whistling:

Posted Image

Click at the bottom-right to confirm the language as HTML5, then wavy-lines should appear. Click on the warning sign (to the left of where the language choice was made) and you can jump between errors.
Was This Post Helpful? 0
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Tossing a coin

Posted 15 February 2013 - 12:27 PM

View Postandrewsw, on 15 February 2013 - 11:57 PM, said:

BTW Two people have now suggested your use of random() is incorrect; well, unless, I'm missing something(?) it isn't :whistling:/>


Well, come to think of it, that use of random() is correct...I don't seem to believe I missed it...I guess I probably missed out a brace...

Thanks for pointing it out, andrewsw :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#13 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Tossing a coin

Posted 15 February 2013 - 12:34 PM

Okay so I am kind of looking for a way to program

if(tossCoin button is clicked)
flip()
if flip is true
++heads
else
++tails
print total of heads and tails

if(exit button is clicked)
print total times coin flipped
print finished heads amount
print finished tails amount
Was This Post Helpful? 0
  • +
  • -

#14 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Tossing a coin

Posted 15 February 2013 - 12:43 PM

Well, you could use a counter variable and increment the counter variable each time the control enters the condition. Like for each time the button is clicked, the counter value is incremented...that will give you total number of flips.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#15 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Tossing a coin

Posted 15 February 2013 - 12:53 PM

My big question is how do I present the user with the toss coin button continuously. Right now the buttons are presented once then the buttons go away and present the number of heads vs tails.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2