Jump to content

code not working


program-3

Recommended Posts

I'm trying to get a general aquantence with JS for my basic knowledge, so I've been doing the "my first java script" section of W3chools. In the if/else section though, I tried to copy the "try it editor" exactly to reproduce what they did, but it doesn't seem to work. I wen't over it line by line, so I don't think it's a simple mistake like not adding a "/" or ">"the link to what I'm reffering to is herehttp://www.w3schools.com/myfirst/js06.aspwhat I did is this

<html><body>My first randomized text: <script type="text/javascript">var r=math.random()if (r>0.5){document.write("<a href='http://www.w3schools.com'target='_blank'>learn web development!</a>")}else{document.write("<a href='http://www.refsnesdata.no'target='_blank'>visit refsnes data!</a>")}</script></body></html>

if it makes a difference, I'm using firefox :?I feel like a real newbi (since I guess thats what I am). Please help point me in the right direction

Link to comment
Share on other sites

I copied your code and ran it in Firefox and, like for you, it didn't work. However, I opened the Javascript Console ("Tools -> Javascript Console") and it told me "math is not defined".Turns out that you have to use "Math" rather than "math":

var r = Math.random();

Link to comment
Share on other sites

so all I have to do is change that one line?I tried it in the "try it editor" and still to no avail...I also used the same code (with the changed line) in a text editor and saved it as an .htm extention, and even that didn't work...I'm sorry if I'm being tedious and asking "stupid" questions

Link to comment
Share on other sites

The only other thing I can think of for you is to make sure that what you have in the document.write() calls are on the same line and do not span multiple lines:

// this should work:document.write("<a href='http://www.refsnesdata.no' target='_blank'>visit refsnes data!</a>");// this may break:document.write("<a href='http://www.refsnesdata.no'target='_blank'>visit refsnes data!</a>")

Link to comment
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...