Shay9999 Posted June 29, 2011 Share Posted June 29, 2011 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. Link to comment Share on other sites More sharing options...
zig zag Posted June 29, 2011 Share Posted June 29, 2011 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. Link to comment Share on other sites More sharing options...
MrFish Posted June 29, 2011 Share Posted June 29, 2011 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. Link to comment Share on other sites More sharing options...
Ingolme Posted June 29, 2011 Share Posted June 29, 2011 Nodes don't have a write() method. You should modify their innerHTML property, or create a new text node and append it. Link to comment Share on other sites More sharing options...
Shay9999 Posted June 29, 2011 Author Share Posted June 29, 2011 Thanks guys! That's all I need to make a text-based adventure =P Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.