6

I need to compare four variables.if any two variables are same i need show an alert. i did like the following way.

var a =1;
var b =2;
var c =3;
var d =4;

if((a==b)||(a==c)||(a==d)||(b==c)||(b==d)||(c==d)){
alert("Value is same, Please change the value");
return false;
}

Is that possible to reduce the expression to one condition instant of checking all the variables separately.Kindly advice ... TnQ in advance.

2
  • You should be using an array. Commented Nov 27, 2012 at 4:37
  • A cryptographic hash is the only way to be sure. Implement SHA-3. Commented Nov 27, 2012 at 5:19

5 Answers 5

1

well, i'll toss this into the ring, albeit it is likely not the right approach to this but it is fun. (note: browser compatibility issues and possibly over-kill is about to precede)

var a = 1,
    b = 2,
    c = 1,
    d = 3;
[a, b, c, d].sort(function(a, b) {
    return a - b;
}).reduce(function(a, b) {
    if (a == b) {
        alert('HERE');
    }
    return b;
});

demo here:
http://jsfiddle.net/rlemon/Eu4qG/

further readings:
Array.reduce
Array.sort

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

9 Comments

While sorting is the efficient way to go about it, reducing is not appropriate here.
But... sorting isn't overkill. It's the best way to do it.
agreed, which is why I still felt the answer was warranted. but I did caution to the caveats of it(reduce to be specific). this is my first inkling to the correct answer and as it is not the best one I had noted that :)
@rlemon Thanks for reminding me about sort! Used it to make my answer more efficient than it was.. I think =)
Thanks for your support.The code working as per expectation. Once again TnQ.
|
1

while is enough. This will work on any length of array. But the a array will be modified, you may need to duplicate.

a = [1, 2, 3, 4];
while (a.length > 1)
  if (a.indexOf(a.pop()) != -1) {
    alert("Value is same, Please change the value");
    break;
  }

My previous answer (in revision), which using for, was little embarrassing.

Comments

0

This checks to see if any of the values are the same as any of the other values:

var values = [a, b, c, d, e, f /* ... */];

function shared_values(values) {
    for (var i = 0; i < values.length; i++) {
        for (var j = i; j < values.length; j++) {
            if (values[i] === values[j]) {
                return true;
            }
        }
    }
    return true;
}

if (shared_values(values)) {
    // Don't submit form or whatever
}

2 Comments

dis is more tedious then OP
@diEcho Only for small value counts. It's the same code whether you have four variables or four hundred.
0

You could write a function to check them for you, maybe something like this:

EDIT: Based on other answers, you could make it more efficient with a sort

function areTwoEqual() {
    var arg_list = [].slice.call(arguments).sort(
        function(a,b) { return a-b; }
    );
    var vals = {};
    for(var i = 0, len = arg_list.length; i < len; i++) {
        if(vals[arg_list[i]]) {
            return true;
        }
        vals[arg_list[i]] = '1';
    }
    return false;
}

var a =1;
var b =2;
var c =3;
var d =1;

if(areTwoEqual(a,b,c,d)) {
    alert('Two values are identical!');
} else {
    alert('all values are unique');
}​

JSFiddle: http://jsfiddle.net/thesouthstar86/j7LmZ/

2 Comments

Hi brian thanks for your support. when more than two values are same it gives two values are identical is that possible tell based on the values it should alert how many values are identical
Yes, you it could be modified to do that, but what result would you expect if your values were something like 1,1,1,3,3,5? would you want it to give you 2? or 3?
0

To avoid a comparison between the same element and itself

if (values[i] === values[j]) { }

I think that it should be upgraded to

if ( (values[i] === values[j]) && (i != j) ) { }

but thanks for the script

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.