较短的代码,而不是对所有组合使用IF

时间:2019-04-11 08:57:06

标签: javascript jquery html

我尝试使用IF添加所有组合,但是要写很多东西。如果我得到用户签名,然后又得到另一个用户签名,例如白羊座和白羊座,则发送输出(“很多”),但是我有12 * 12 = 144个组合!

是否可以选择缩短时间?

    <script>
 function hatama() {
       signYours= $("input[name='sign1']").val()
       signFriend=$("input[name='sign2']").val()


       //Aries
       if (signYours=="Aries" && signFriend=="Gemini")|| (signYours=="Aries" && signFriend=="Leo")|| (signYours=="Aries" && signFriend=="Saggitarious")|| (signYours=="Aries" && signFriend=="Libra")||(signYours=="Aries" && signFriend=="Aquarius")
       $("#output").val("High");
       if (signYours=="Aries" && signFriend=="Aries")||(signYours=="Aries" && signFriend=="Virgo") || (signYours=="Aries" && signFriend=="Capricon") || 
       $("#output").val("Medium");
       if (signYours=="Aries" && signFriend=="Pisces") || (signYours=="Aries" && signFriend=="Cancer")|| (signYours=="Aries" && signFriend=="Scorpio")
       $("#output").val("Low");
       //Taurus 
       if (signYours=="Taurus" && signFriend=="Virgo")|| (signYours=="Taurus" && signFriend=="Capricon")||(signYours=="Taurus" && signFriend=="Taurus")||(signYours=="Taurus" && signFriend=="Cancer")||(signYours=="Taurus" && signFriend=="Scorpio")||(signYours=="Taurus" && signFriend=="Pisces")
       $("#output").val("High");
       if (signYours=="Taurus" && signFriend=="Aquarius")||(signYours=="Taurus" && signFriend=="Taurus")
       $("#output").val("Medium");
       if (signYours=="Taurus" && signFriend=="Gemini")|| (signYours=="Taurus" && signFriend=="Leo")||(signYours=="Taurus" && signFriend=="Saggitarious")
       $("#output").val("Low");


</script>

2 个答案:

答案 0 :(得分:3)

一个选择是使用一个对象,该对象的属性是您的符号,其值是具有HighMediumLow属性的对象,其值是相关的符号:

// Object indexed by your own sign:
const signs = {
  Aries: {
    High: ['Gemini', 'Leo', 'Saggitarious', 'Libra', 'Aquarius'],
    Medium: ['Aries', 'Virgo', 'Capricon'],
    Low: ['Pisces', 'Cancer', 'Scorpio']
  }
  // ...
};
const getAssoc = (signYours, signFriend) => {
  const yourObj = signs[signYours];
  if (!yourObj) {
    return "Your sign is invalid";
  }
  const foundEntry = Object.entries(yourObj)
    .find(([_, arr]) => arr.includes(signFriend));
  if (!foundEntry) {
    return "Friend's sign is invalid";
  }
  return foundEntry[0];
};
console.log(getAssoc('Aries', 'Gemini'));
console.log(getAssoc('Aries', 'Scorpio'));

因此,对于您的代码:

function hatama() {
  const signYours = $("input[name='sign1']").val();
  const signFriend = $("input[name='sign2']").val();
  $("#output").val(getAssoc(signYours, signFriend));
}

(请确保始终声明变量,否则将在全局对象上隐式创建属性,应避免使用该值)

如果需要为每个可能的符号在signs上设置属性,则可能有一种更有效的方法,如果存在高/中/低关联的模式。

答案 1 :(得分:1)

恕我直言,这里的问题是要有一个结构良好的数据,而不是一系列的IF语句,请按配置检查我的解决方案:

const signs = [
  {
    userSign: "Aries",
    associations: [
      {
        signs: ["Gemini","Leo", "Sagittarius", "Libra", "Acquarius"],
        val: "High",
      },
    // further match
    ]
  },
  // furter signs
 ]

getMatchValue = (user1, user2) => {
   let result;
   const user1Sign = signs.find(
     (sign) => sign.userSign === user1
   );
   user1Sign.associations.forEach(
    (association) => {
      if(association.signs.includes(user2)){
        result = association.val;
      }
    }
   )
   return result;
}

console.log(getMatchValue("Aries", "Gemini"));