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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    79
    Thanks
    2
    Thanked 1 Time in 1 Post

    easy q - condition

    Hello everyone!

    My error must be extremely simple, but can't solve this puzzle., Please help.

    Code:
    var cause, effect; cause = window.prompt("?"); Function { if (cause = "1") {effect = "1"} if (cause = "2") { effect = "4" } document.writeln(effect)
    please help, returns the same value.
    Thank you.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    Bad:
    Code:
    if ( cause = "1" )
    In JavaScript, the = operator is *ALWAYS* an assignment operator. *ALWAYS*. Even in an if condition. To *TEST* the value of something you MUST use the == operator.
    Code:
    if ( cause == "1" )
    *************************************
    Bad:
    Code:
    Function
    (a) JavaScript is case sensitive. Function is *NOT* the same as function.
    (b) You didn't name the function.
    (c) You didn't invoke the function.
    (d) You have a left brace { but no matching right brace }.

    But you don't really need or want a function, anyway. Just remove the word Function and the first left brace { entirely and it will run.

    It's really bad code, because you are using prompt and document.write, both of which should be avoided, but since I assume this is Lesson 1 in Week 1 of your course, it's acceptable.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    79
    Thanks
    2
    Thanked 1 Time in 1 Post
    Thank you very much. I was in a dead spot. It is a different thing to pick out the basics from "JS in 10 minutes" type of courses vs to identify problems.


  •  

    Posting Permissions

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