Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Errors trying to run basic .js file

    Hey guys. I am new to javascript and going through the beginning stages of learning it all. I have a few lines of code that worked in the courses browser, but when I saved the same file as a .js and attempted to run it, I repeatedly got a 800A138F error, object expected at line 1 character 1.

    Code:

    var finish = prompt("Did you finish the course?")
    var finish = finish.toLowerCase()
    var yes = "yes"
    var no = "no"
    var victory = "I finished my first course!"
    var defeat = "Go finish the course."
    var wrong = "Yes or No are the only acceptable answers."
    if (finish === yes)
    {
    console.log(victory);
    }
    else if (finish === no)
    {
    console.log(defeat);
    }
    else if (finish !== no || finish !== yes)
    {
    console.log(wrong);
    }

    I know it's kind of really simple code but I'd like to know what I'm doing wrong to avoid this error in the future or what would allow the script to run as a .js. The same code did work when I changed out the console.log's for document.write in html. It also worked as is in the browser course test area

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Look at this and identify the logic error:-

    else if (finish !== no || finish !== yes); // no != yes!!

    Otherwise your script seems to work fine. But be aware that prompts and alerts (console.log) and document.write are obsolete and should be used only for testing purposes.

    If you place your Javascript in an external file, that file must not contain any HTML tags. Such as <script>

    He [Smith] did everything right, he just missed the ball. - Commentator BT Sport
    Last edited by Philip M; 06-04-2014 at 03:49 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh crap! Derp. Should use &&, other makes no sense

    What is used in place of prompts/alerts then?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Van Tango View Post

    What is used in place of prompts/alerts then?
    DOM methods - input boxes and document.getElementById("elementid").innerHTML or value

    Something as simple as "Did you finish the course?" with only two options is best done with radio buttons. Then no further validation is needed.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •