×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

easy

<script type ="text/javascript" >
var VISA = 0
var MC = 1
var AE = 2
var DISCOVER = 3

var A = new Array{true, true, true, false};
var B = new Array{true, false, true, false};
var C = new Array{true, true, false, true};

if (A[VISA] && B[VISA] && C [VISA]) {
// VISA is the common type
}

if (A[MC] && B[MC] && C [MC]) {
// MC is the common type
}

if (A[DISCOVER] && B[DISCOVER] && C [DISCOVER]) {
// MC is the common type
}

if (A[DISCOVER] && B[DISCOVER] && C [DISCOVER]) {
// MC is the common type
}
</script>
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 请教,请教。问一个javascript的问题:
    问题比较简单,描述起来比较麻烦。简单地说,如果有多个商家,每个商家接受不同的信用卡,如下:

    store A visa, mc, AE
    store B visa, AE
    store C visa, mc, discover

    这样,当用户选择不同的商家的时候,需要找出他们的common payment type,比如,如果选了A and C, 则visa, mc应该被摘出来;如果选了A and B, then visa,AE should be selected。
    这些都要求在javascript里面做,我弄了几个数组了,不过,现在把自己给绕进去了。:))

    希望找个最简洁的方法。。。。。

    谢谢大家。:)
    • 能不能这样
      1、建立一个新数组,用来记录最后的信用卡名单。

      2、扫描所有选中的商家,第一个商家的所有信用卡都放进新数组。以后的商家里面的每个信用卡都拿出来和新数组里面的比较,如果没有发现,就将此卡从新数组中剔除。

      3、重复到最后,新数组里面应该就只有大家都有的信用卡了。
      • 当然这么做过。有问题的。
    • array 1:: store_id: card1(0 for no, 1 for yes), card2,....cardN; array2: store_id. working variale0: int stores; working varialble2: int VISA; working variable3: MASTER.... working variableN: nnnncard.
      #initilize working variables.
      working_variable0= working_variable1=.... = working_variable0=0;
      for (a_store_id in array2){
            working_variable0++;
            if (array1[a_store_id].card1) working_variable1++;
            if (array1[a_store_id].card2) working_variable2++;
            if (array1[a_store_id].card3) working_variable3++;
            ...
            if (array1[store_id].cardN) working_variableN++;
      }
      
      if (workong_varialbe0 == working_varialbe1) show_up(card1);
      if (workong_varialbe0 == working_varialbe2) show_up(card2);
      ...
      if (workong_varialbe0 == working_varialbeN) show_up(cardN);
      
      
    • Create a JSON object. It's the easiest way.
    • javascript 支持位运算吗?两个整数and运算就出来了
    • JavaScript Operators..
      • 谢谢。水缸里也这么提醒我了,正在考虑。:)
    • ...
      function getCardList(store) {
      var cardList;
      if (store== 'A') {
      cardList = {'visa', 'mc', 'AE'};
      } else if ....
      }
      return cardList;
      }
      function populateCardBox(cardList) {...}

      function onStoreSelect(obj){
      var store = obj[selectedIndex].selectedValue;
      populateCardBox( getCardList(store));
      }


      <select ... onchange="onStoreSelect(this);">
    • easy
      <script type ="text/javascript" >
      var VISA = 0
      var MC = 1
      var AE = 2
      var DISCOVER = 3

      var A = new Array{true, true, true, false};
      var B = new Array{true, false, true, false};
      var C = new Array{true, true, false, true};

      if (A[VISA] && B[VISA] && C [VISA]) {
      // VISA is the common type
      }

      if (A[MC] && B[MC] && C [MC]) {
      // MC is the common type
      }

      if (A[DISCOVER] && B[DISCOVER] && C [DISCOVER]) {
      // MC is the common type
      }

      if (A[DISCOVER] && B[DISCOVER] && C [DISCOVER]) {
      // MC is the common type
      }
      </script>
      • better one
        本文发表在 rolia.net 枫下论坛<html xmlns="http://www.w3.org/1999/xhtml" >
        <head>
        <title>Untitled Page</title>
        <script type ="text/javascript" >
        function show(){
        var VISA = 0;
        var MC = 1;
        var AE = 2;
        var DISCOVER = 3;

        var A = new Array(true, true, true, false);
        var B = new Array(true, false, true, false);
        var C = new Array(true, true, false, true);
        var common_type = new Array(4);

        for (var i = 0; i<4; i++){
        common_type[i] = A[i] && B[i] && C[i];
        }

        var str = '';
        if ( common_type[VISA] ) {
        // VISA is the common type
        str += 'VASIA ';
        }

        if ( common_type[MC] ) {
        // MC is the common type
        str += 'MC ';
        }

        if ( common_type[AE] ) {
        // AE is the common type
        str += 'AE ';
        }

        if ( common_type[DISCOVER] ) {
        // DISCOVER is the common type
        str += 'DISCOVER ';
        }
        document.getElementById ('result').innerText = str;
        }
        </script>
        </head>
        <body>
        <input id="Button1" type="button" value="button" onclick ='show();' />
        <span id='result'></span>

        </body>
        </html>更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • 呵呵,如果里面都是动态的,就没有那么easy了。anyway,我最后用了3个数组,3个循环给弄出来了。:)thanks.