-1

I have a problem using quotation marks in js... I have an input field using this js-function

    function validate(xyz) {
            "+umum+" == "yeah_it_is_ok";
            if(xyz == ""+umum+"") {
                  alert("Hoera!");
                  return true;
            } else {
                  alert("Too bad!");
                  return false;
            }
     } 

What do I have to insert in the input-field to get the Hoera message?$

In other words, what is the function of a " or a + in js?

5
  • What do you mean by "the input-field"? Your question is not too clear. Commented Jul 12, 2011 at 15:27
  • 3
    Please read a JavaScript language reference. You clearly need to understand JavaScript basics. (And: try it! Use your JavaScript debugger/console which you should have - e.g. Web Console or Firebug in Firefox 4+, Developer Tools in Webkit browsers/IE) Commented Jul 12, 2011 at 15:28
  • and while you are at it (reading a javascript reference ) pay special attention to rules for real identifier Commented Jul 12, 2011 at 15:34
  • @Chris Morgan, my JS debugger doesn't show any errors... But thanks for the links. I'll try it Commented Jul 12, 2011 at 15:35
  • @Michiel: I mean things like evaluating this in your console: ""+umum+"", and var umum = "foo" followed by ""+umum+"", and things like that. Use it to get interactive feedback on what it does. Experiment. Commented Jul 12, 2011 at 15:48

5 Answers 5

2

You don't have a syntax error in the function declaration, but it will fail at execution time, because umum is not defined; and surely you have a semantic error, because the only way to get "Hoera" is to declare the umum var first and call the validate function later:

var umum;
validate("test value");

Of course, it always give a "too bad!" message unless you pass ""+undefined+"" as parameter. I think the right function should be:

function validate(xyz) {
        var umum = "yeah_it_is_ok"; // or whatever you want to validate with..
        if(xyz == umum) {
              alert("Hoera!");
              return true;
        } else {
              alert("Too bad!");
              return false;
        }
 }

In this case, when calling validate("yeah_it_is_ok") you'll get an "Hoera!".

Sign up to request clarification or add additional context in comments.

Comments

2

You would want to declare a variable like this:

var umum = "yeah_it_is_ok";

Note the var keyword and the use of a single equals for assignment.

Also, a pair of " characters is used to enclose a string variable, and the + will concatenate two strings. However, if you wish to have a double-quotation within a string you need to escape it with a backspace \. For example:

if(xyz == "\"+umum+\"") {

4 Comments

is the use of varobligated to declare a variable in js?
No, but it is recommended because if you omit var you will declare a global variable. See the Declaring Variables section in the MDN docs: developer.mozilla.org/en/JavaScript/Guide/…
See my edits, I meant a string is enclosed in double quotes like so: "mystring". Although a pair of single quotes may be used as well.
As others have said, you might want to read a beginner's JavaScript book to get familiar with the basics.
1

Single- and double-quote characters are used to delimit string constants. The + character is an operator that serves several purposes, including string concatenation, numeric addition, and asserting numeric "positiveness" (used often for its implicit side effects).

4 Comments

so what do I need to input in my input-field to get the Hoera message?
You still have not explained what you mean by "input-field". The code you posted is so nonsensical that I have no idea what it is you're trying to do.
I have an input field with a button. Once a user clicks the button, the above js function is used to validate the input. But what do I need to input to get the Hoera message?
The code you posted does not make any sense. You'll have to try and explain what it's supposed to do. You also have not said how exactly your input element is related to this function.
1

I think you mean to write your function like this.

function validate(xyz) {
    umum = "yeah_it_is_ok";

    if(xyz == umum) {
        alert("Hoera!");
        return true;
    } else {
        alert("Too bad!");
        return false;
    }
}

So then to answer your question, you can put the string that your looking for into the input-field. Which, since you don't have an input field in your example, we can just call the function with the correct string.

validate("yeah_it_is_ok");

Also it seems like you were thinking that you can use " or + in a variable. You can't do that. As others have suggested, you should learn the basics of JavaScript. w3schools.com and the Mozilla Developer Network are good places to do that.

http://www.w3schools.com/js/default.asp

https://developer.mozilla.org/en-US/learn/javascript

Comments

0

I believe you put a \ infront of it so like

    if(xyz == "\"+umum+\"") {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.