Compare Multiple variables in javascript - Stack Overflow

I need to pare four variables.if any two variables are same i need show an alert.i did like the follow

I need to pare 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.

I need to pare 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.

Share Improve this question asked Nov 27, 2012 at 4:33 AbdulAbdul 1,0503 gold badges16 silver badges33 bronze badges 2
  • You should be using an array. – Waleed Khan Commented Nov 27, 2012 at 4:37
  • A cryptographic hash is the only way to be sure. Implement SHA-3. – temporary_user_name Commented Nov 27, 2012 at 5:19
Add a ment  | 

5 Answers 5

Reset to default 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 patibility 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/rlemon/Eu4qG/

further readings:
Array.reduce
Array.sort

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.

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
}

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/thesouthstar86/j7LmZ/

To avoid a parison 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744721532a4589939.html

相关推荐

  • Compare Multiple variables in javascript - Stack Overflow

    I need to pare four variables.if any two variables are same i need show an alert.i did like the follow

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信