www.webdeveloper.com
Results 1 to 3 of 3

Thread: I need help D=

  1. #1
    Join Date
    Sep 2013
    Posts
    1

    I need help D=

    I don't understand why this javascript isn't working....
    var value = prompt("Hi! How are you?", "Answer good, bad or ok.");
    var answer = (value);
    var greeting = function(answer) {
    if (answer === good)
    {
    console.log("That's great! Have a nice day!");
    }
    else if (answer === bad)
    {
    console.log("Aw =/ I hope things get better for you soon!");
    }
    else if (answer === ok)
    {
    console.log("That's good! Have a nice day!");
    }
    else
    {
    console.log("Sorry, I don't understand.");
    }
    };
    greeting();

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    If you write a function that takes a parameter, it's advisable to pass it one.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    You must pass parameter to the greeting function and you must put (good, bad, ok) in quotation marks.

    Code:
    var value = prompt("Hi! How are you?", "Answer good, bad or ok.");
    var answer = (value);
    var greeting = function(answer) {
    if (answer === "good")
    {
    console.log("That's great! Have a nice day!");
    }
    else if (answer === "bad")
    {
    console.log("Aw =/ I hope things get better for you soon!");
    }
    else if (answer === "ok")
    {
    console.log("That's good! Have a nice day!");
    }
    else 
    {
    console.log("Sorry, I don't understand.");
    }
    };
    greeting(answer);
    although using the same name for variables and function parameters is not an error it's not considering as a good coding practice.

    You could define your function like this if you are using "answer" variable to store input from the user

    Code:
    var greeting = function(a){...}
    greeting(answer)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles