Abhishek Mhatre
Abhishek Mhatre

Reputation: 557

how to find the variable which is empty and display it

I have three variable a,b and c in JavaScript and one of them is null. How do I find which one it is and then display it without if statements as I could have not 3 but even 300 variables. I tried to use a loop but with no luck. Thanks in Advance. My code uses a array it is;

var x = [a,b,c];
for (i = 0; i < x.length; i++){
    if (x[0] == null){
        alert(x[0]);
    }
}

However this only checks for null items but can't display them.

Upvotes: 1

Views: 107

Answers (6)

Snowburnt
Snowburnt

Reputation: 6922

assuming you can't use arrays or objects for some strange reason..

function findNull(){
    for (var i=0; i<arguments.length;i++){
        if (arguments[i]===null) {
           return i;
        }
    }
}

if you call it like this:

findNull(a,b,c,d,e,f,g);

and d is null and you know you only have one null it will return 3, since that's the index it was sent.

EDIT:

in response to your edit:

try

x = {"a":a; "b":b; "c":c};
// you can add more values like this
x["d"]=d;
for (var i in x){
    if (x[i]===null){
       alert (i);
    }
}

it will alert the name of the key (if b is null it will prompt b)

Upvotes: 0

kybernetikos
kybernetikos

Reputation: 8689

It's likely that you should be using properties of an object rather than local variables in an array.

var x = {
    a: 23,
    b: null,
    c: 26
}

// your code can work with these values by doing 
// e.g. x.a++ instead of a++

for (var key in x) {
    if (x[key] === null) {
        alert(key);
    }
}

Upvotes: 0

Billy Moon
Billy Moon

Reputation: 58541

Hard to be sure exactly what you are looking for - but perhaps it is this...

var x = [a,b,c];
for(i=0;i<x.length;i++){
    // reference the `i`th item rather than the 0th item each time
    if (x[i] == null){
        alert(x[i]);
    }
}

perhaps you are trying to avoid...

if (x[0] == null){
    alert(x[0]);
}
if (x[1] == null){
    alert(x[1]);
}
...

Upvotes: 0

Levi Beckman
Levi Beckman

Reputation: 533

Try this:

    var x = ['a', 'b', null];
    var i = x.indexOf(null);
    alert(i);

It works with vars:

    var a = 'foo';
    var b = 'bar';
    var c = null;
    var x = [a, b, c];
    var i = x.indexOf(null);
    alert(i);

Upvotes: 1

Tyler Biscoe
Tyler Biscoe

Reputation: 2422

Using arrays:

As somone suggested you can use an array, storing the values like so:

var values = [1, 2, 3, 4];

You can access the values like so:

values[0]; // equals 1
values[3]; // equals 4

And loop through them like so:

for(var i = 0; i < values.length; i++) {
    console.log(values[i]);
}

Outputs:
    1
    2
    3
    4

Using objects:

If it were me, though, I would use an object instead that way you can reference the object values by names (properties) instead of just by index. Of course, objects aren't the right tool for every job, but based on your vague description, it sounds like what you will need.

Create an object like so:

var someObj = {
    firstValue: 1,
    secondValue: 2,
    anotherValue: 3
};

Values can be accessed like so:

someObj.firstValue; // equals 1

or:

someObj['anotherValue']; // equals 3

And can be looped through like so:

for(var ind in someObj) {
    console.log(someObj[ind]);
}

Outputs:
    1
    2
    3

Hope that helps.

Upvotes: 0

Darren
Darren

Reputation: 70728

Try holding the variables in an array and iterate over each element and check if it is null:

var arr = [];
var obj1 = 'davies';
var obj2 = 'darren';
var obj3 = null;

arr.push(obj1);
arr.push(obj2);
arr.push(obj3);

for (var i =0; i<arr.length; i++) {
    if (arr[i] == null) {
       alert("index " + i + " is null");   
    }
}

http://jsfiddle.net/75Xwh/

Upvotes: 0

Related Questions