.write and randomizations

Recommended Posts

I am just starting to get into this, but I already flunked. How do I do this:

`<html><head><script type="text/javascript">function dothis(){document.getElementById("action").write("Yes.");//This doesn't seem to work.}</script></head><body><p id="action">No.</p><button type="button" onclick="dothis()">Yes?</button></body></html>`

Also, I didn't see this in the tutorial page, but is there a way to make a random number and assign it to a variable? Please respond, thank you for your time.

Edited by Shay9999
Share on other sites

Try this

`<html><head><script type="text/javascript">function dothis(){document.getElementById('action').innerHTML = 'Yes';};</script></head><body><p id="action">No.</p><button type="button" onclick="dothis()">Yes?</button></body></html>`

and yes there is a way to assign a random number to a variable.var randomNumber=Math.floor(Math.random()*6) // The variable "randomNumber" will equal a random number between 0-5var randomNumber=Math.floor(Math.random()*11) // The variable "randomNumber" will equal a random number between 0-10Now someone correct me if I'm wrong but remember the following...Declaring this variable inside a function will generate a new number every time you run the function.Declaring this variable outside all functions will generate a new number every time you load the page.

Share on other sites

Now someone correct me if I'm wrong but remember the following...Declaring this variable inside a function will generate a new number every time you run the function.Declaring this variable outside all functions will generate a new number every time you load the page.
That's correct.Also, Math.random() generates a decimal between 0 and 1 (like 0.2248631827533245) which is why you have to multiply it by the maximum amount you are looking for +1. Just thought I should note that since it might be obvious.
Share on other sites

Nodes don't have a write() method. You should modify their innerHTML property, or create a new text node and append it.

Share on other sites

Thanks guys! That's all I need to make a text-based adventure =P

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account