希望能够获得javascript的所有可能的ABCD组合

时间:2015-04-27 16:06:08

标签: javascript

问题是我有一个javascript查看我的文本字段类' setup'此字段可以按任何顺序或组合包含以下任何字母A,B,C,D。我希望能够设置每个组合的价值是他们这样做的简单方法而不是硬编码所有340种组合......请任何帮助将不胜感激!!!!

这是我的代码

$(document).ready(function(){

  $('.scrap').click(function() {
    var weld_scrap = $('.weld').val();
    var dim_scrap = $('.dim').val();
    var visual_scrap = $('.visual').val();
    var adjust_scrap = $('.adjust').val();
    var setup = $('.setup').val();
    var A = '10';
    var B = '10';
    var C = '10';
    var D = '10';
var AA = '20';
var AB = '20';
var AC = '20';
var AD = '20';
var BA = '20';
var BB = '20';
var BC = '20';
var BD = '20';
var CA = '20';
var CB = '20';
var CC = '20';
var CD = '20';
var DA = '20';
var DB = '20';
var DC = '20';
var DD = '20';
var AAA = '30';
var AAB = '30';
var AAC = '30';
var AAD = '30';
var ABA = '30';
var ABB = '30';
var ABC = '30';
var ABD = '30';
var ACA = '30';
var ACB = '30';
var ACD = '30';
var ADA = '30';
var ADB = '30';
var ADC = '30';
var ADD = '30';
var BAA = '30';
var BAB = '30';
var BAC = '30';
var BAD = '30';
var BBA = '30';
var BBB = '30';
var BBC = '30';
var BBD = '30';
var BCA = '30';
var BCB = '30';
var BCC = '30';
var BCD = '30';
var BDA = '30';
var BDB = '30';
var BDC = '30';
var BDD = '30';
var CAA = '30';
var CAB = '30';
var CAC = '30';
var CAD = '30';
var CBA = '30';
var CBB = '30';
var CBC = '30';
var CBD = '30';
var CCA = '30';
var CCB = '30';
var CCC = '30';
var CCD = '30';
var CDA = '30';
var CDB = '30';
var CDC = '30';
var CDD = '30';
var DAA = '30';
var DAB = '30';
var DAC = '30';
var DAD = '30';
var DBA = '30';
var DBB = '30';
var DBC = '30';
var DBD = '30';
var DCA = '30';
var DCB = '30';
var DCC = '30';
var DCD = '30';
var DDA = '30';
var DDB = '30';
var DDC = '30';
var DDD = '30';
    if (setup == 'A') {
      setup = A;
    }else if (setup == 'B') {
      setup = B;
    }else if (setup == 'C') {
      setup = C; 
    }else if (setup == 'D') {
      setup = D; 
    }else if (setup == 'AA') {

    }
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup);  
   console.log(sum);
    $('.scrap').val(sum);
  });
});

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

var pArr = [],
  usedChars = [];

function permute(input) {
  var index, char;
  for (index = 0; index < input.length; index++) {
    char = input.splice(index, 1)[0];
    usedChars.push(char);
    if (input.length == 0) {
      pArr.push(usedChars.slice());
    }
    permute(input); //Recursive
    input.splice(index, 0, char);
    usedChars.pop();
  }
  return pArr
};


document.write(JSON.stringify(permute(["A", "B", "C", "D"])));