0

I'm trying the following code:

var var1 = "";
var var2 = "test";
var var3 = "";

vars = new Array('var1','var2','var3');

var count = 0;

for (var i = 0; i < vars.length; ++i) {
    var name = vars[i];
    if (field_is_empty(name)) {
        count++;
    }
}

console.log(count);

function field_is_empty(sValue) {
    if (sValue == "" || sValue == null || sValue == "undefined")
    {
        return true;
    }
    return false;
}

The result here should have been count = 2 because two of the variables are empty but it's always 0. I guess it must something when using if (field_is_empty(name)) because it might not getting the name converted to the name of the actual var.

PROBLEM 2# Still related

I've updated the code as Karthik Ganesan mentioned and it works perfectly.

Now the code is:

var var1 = "";
var var2 = "test";
var var3 = "";

vars = new Array(var1,var2,var3);

var count = 0;

for (var i = 0; i < vars.length; ++i) {
    var name = vars[i];
    if (field_is_empty(name)) {
        count++;
    }
}

console.log(count);

function field_is_empty(sValue) {
    if (sValue == "" || sValue == null || sValue == "undefined")
    {
        return true;
    }
    return false;
}

And the problem is that if add a new if statement something like this:

if (count == '3') {
    console.log('AllAreEmpty');
} else {
    for (var i = 0; i < vars.length; ++i) {
        var name = vars[i];
        if (field_is_empty(name)) {
            //Set the empty variables as "1900-01-01"
            variableService.setValue(name,"test");
        }
    }
}

It does nothing and I've tested using variableService.setValue('var1',"test") and it works.

PS: The variableService.setValue is a function controlled by the software I don't know exactly what it does I know if use it like mentioned on above line it works.

3
  • 1
    It's because you're not passing the content of each variable, but rather the name of it. For example, you're doing field_is_empty('var1'), not field_is_empty(""). You simply need to remove the quotation marks from your array. Commented Dec 16, 2016 at 18:01
  • 1
    You're not properly putting your variables into your array. Look closer at what you're doing, which is putting a string of the variable name, but not the variable itself. Remove the quotes for starters. Commented Dec 16, 2016 at 18:01
  • Use: vars = new Array(var1, var2, var3);. Commented Dec 16, 2016 at 18:01

3 Answers 3

1

In your first attempt you used the variable names as strings when you created an array. You need to either use the values themselves:

vars = new Array(var1,var2,var3);

or if you insist to use them by their names, then you need to find them by names when you use them:

if (field_is_empty(window[name])) {

It does nothing

That's not really possible. It could throw an error, or enter the if or enter the else, but doing nothing is impossible. However, since you intended to use the variables by name in the first place (probably not without a reason) and then you intend to pass a name, but it is a value and it does not work as expected, I assume that your initial array initialization was correct and the if should be fixed like this:

var var1 = "";
var var2 = "test";
var var3 = "";

vars = new Array(var1,var2,var3);

var count = 0;

for (var i = 0; i < vars.length; ++i) {
    var v = window[vars[i]]; //You need the value here
    if (field_is_empty(v)) {
        count++;
    }
}

console.log(count);
if (count == '3') {
    console.log('AllAreEmpty');
} else {
    for (var i = 0; i < vars.length; ++i) {
        var v = window[vars[i]];
        if (field_is_empty(v)) {
            //Set the empty variables as "1900-01-01"
            variableService.setValue(vars[i],"test");
        }
    }
}

function field_is_empty(sValue) {
    if (sValue == "" || sValue == null || sValue == "undefined")
    {
        return true;
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You definitely incorrectly initialize array, you put strings "var1", "var2", "var3" instead of references to strings (variables). Try this:

vars = new Array(var1,var2,var3);

Comments

0

Your array is wrong

it should be

vars = new Array(var1,var2,var3);

here is the jsfiddle

5 Comments

That worked ok but it created me another problem, if you could please take a look at edit. Anyhow thanks for your tip.
where are you adding this "if" condition?
Right before function field_is_empty
by the way you said try something like this to see if it works variableService.setValue("'"+name+"'","test");
sorry i just saw you need the variable name

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.