9 Replies Latest reply on Dec 30, 2012 12:19 AM by Test Screen Name

    && error

    m.Rodin

      I'm used to php programming and in an if statement a && means both... Everything in my script below after the && isn't working only the frist item in the string. Please advice?!

       

       

      if (this.getField("Weapon_Mele_1").value === 'Gauntlet (Tiny)' && 'Strike, unarmed (Tiny)') {

      event.value = '1'

      } else if (this.getField("Weapon_Mele_1").value === 'Gauntlet (Small)' && 'Strike, unarmed (Small)') {

      event.value = '1d2'

      } else if (this.getField("Weapon_Mele_1").value === 'Gauntlet (Medium)') {

      event.value = '1d3'

      } else if (this.getField("Weapon_Mele_1").value === 'Gauntlet (Large)' && 'Strike, unarmed (Large)' && 'Dagger' && 'Dagger, punching' && 'Gauntlet, spiked' && 'Hammer, light' && 'Pick, light' && 'Faen nunchaku' && 'Main gauche') {

      event.value = '1d4'

      } else if (this.getField("Weapon_Mele_1").value === 'Sprytestaff' && 'Spryte swordstaff') {

      event.value = '1d4/1d4'

      } else if (this.getField("Weapon_Mele_1").value === 'Gauntlet, spiked (giant\'s)' && 'Mace, light' && 'Sickle' && 'Club' && 'Halfspear' && 'Axe, throwing' && 'Handaxe' && 'Lance, light' && 'Sap' && 'Sword, short' && 'Pick, heavy' && 'Rapier' && 'Scimitar' && 'Faen swordaxe' && 'Battle claw' && 'Nunchaku') {

      event.value = '1d6'

      } else if (this.getField("Weapon_Mele_1").value === 'Faen staff' && 'Quarterstaff' && 'Faen swordstaff') {

      event.value = '1d6/1d6'

      } else if (this.getField("Weapon_Mele_1").value === 'Mace, heavy' && 'Morningstar' && 'Shortspear' && 'Battleaxe' && 'Flail, light' && 'Lance, heavy' && 'Longsword' && 'Trident' && 'Warhammer' && 'Longspear' && 'Swordaxe' && 'Greater battle claw' && 'Spikestick') {

      event.value = '1d8'

      } else if (this.getField("Weapon_Mele_1").value === 'Greatstaff' && 'Double-headed flail' && 'Litorian warclub' && 'Swordstaff') {

      event.value = '1d8/1d8'

      } else if (this.getField("Weapon_Mele_1").value === 'Greatclub' && 'Flail, heavy' && 'Glaive' && 'Halberd' && 'Greatspear' && 'Mangler, light' && 'Sibeccai kopesh' && 'Sword, *******' && 'Waraxe') {

      event.value = '1d10'

      } else if (this.getField("Weapon_Mele_1").value === 'Giant\'s swordstaff') {

      event.value = '1d10/1d10'

      } else if (this.getField("Weapon_Mele_1").value === 'Maul (giant\'s mace)' && 'Greataxe' && 'Hammer, heavy') {

      event.value = '1d12'

      } else if (this.getField("Weapon_Mele_1").value === 'Falchion' && 'Guisarme' && 'Ranseur' && 'Scythe') {

      event.value = '2d4'

      } else if (this.getField("Weapon_Mele_1").value === 'Giant\'s club' && 'Greatsword' && 'Great halberd' && 'Mangler, heavy' && 'Spikestick, long') {

      event.value = '2d6'

      } else if (this.getField("Weapon_Mele_1").value === 'Giant\'s axe' && 'Giant\'s sword' && 'Great maul') {

      event.value = '2d8'

      } else { event.value = '000d000'}

       

        • 1. Re: && error
          try67 MVP & Adobe Community Professional

          You have both a syntax error and a logical error.

          First of all, in JS you have to specify the full expression in both sides of the logical operator, so you have to use:

          if (a==1 && a==2) {}

          Not:

          if (a==1 && 2) {}

          This demonstrates your logical error as well. How can the same variable be equal to two different values at the same time? If you used the OR operator (||) it would make more sense...

          • 2. Re: && error
            m.Rodin Level 1

            For the sake of a long script, I've shortened it down to a few lines.

             

             

            if (this.getField("Weapon_Mele_1").value == 'Gauntlet (Tiny)' || 'Strike, unarmed (Tiny)') {

            event.value = '1'

            } else if (this.getField("Weapon_Mele_1").value == 'Gauntlet (Small)' || 'Strike, unarmed (Small)') {

            event.value = '1d2'

            } else if (this.getField("Weapon_Mele_1").value == 'Gauntlet (Medium)') {

            event.value = '1d3'

            } else { event.value = '000d000'}

             

            When I do the above, everything equals one the first value.

            • 3. Re: && error
              try67 MVP & Adobe Community Professional

              Again, your syntax is incorrect. You have to have a full comparison in both sides of the operator. So this:

              if (this.getField("Weapon_Mele_1").value == 'Gauntlet (Tiny)' || 'Strike, unarmed (Tiny)') {

              Should be:

              if (this.getField("Weapon_Mele_1").value == 'Gauntlet (Tiny)' || this.getField("Weapon_Mele_1").value == 'Strike, unarmed (Tiny)') {

              • 4. Re: && error
                m.Rodin Level 1

                Thanks try67, this helped. I was origninally trying to get away from having to type out this.getField("Weapon_Mele_1").value ==

                over and over again. Guess I gotta, hehe.

                • 5. Re: && error
                  try67 MVP & Adobe Community Professional

                  Actually, you don't have to do it. You can assign that value to a variable, like so:

                  var wm1 = this.getField("Weapon_Mele_1").value;

                  if (wm1 == 'Gauntlet (Tiny)' || wm1 == 'Strike, unarmed (Tiny)') {

                  • 6. Re: && error
                    George_Johnson MVP & Adobe Community Professional

                    Consider a different approach that will be much easier to maintain. In your original code you're getting the value of the same field numerous times. You just need to do this once at the beginning of the script, placing the value in a variable, and using the variable in the subsequent code. I would also suggest placing all of the result values in an object so they can simply be looked up rather than that long series of if/then/else if statements. Something like:

                     

                    // Get the value of the field

                    var val = getField("Weapon_Mele_1").valueAsString;

                     

                    // Object to associate field value with result

                    // This really should be in a document-level script that gets loaded when the document is loaded

                    var oR = {};

                    oR["Gauntlet (Tiny)"] = "1";

                    oR["Strike, unarmed (Tiny)"] = "1";

                    oR["Gauntlet (Small)"] = "1d2";

                    oR["Strike, unarmed (Small)"] = "1d2";

                    oR["Gauntlet (Medium)"] = "1d3";

                    oR["Gauntlet (Large)"] = "1d4";

                    oR["Strike, unarmed (Large)"] = "1d4";

                    // etc.

                    oR["default"] = "000d000";

                     

                    // Get the result from the object

                    event.value = oR[val] || oR.default;

                    • 7. Re: && error
                      m.Rodin Level 1

                      Yeah, that's what I ended up doing. THanks

                      • 8. Re: && error
                        m.Rodin Level 1

                        Thanks George, I'll concider this when I have to do more complex drop menu's that have a lot more stats to them.     

                        • 9. Re: && error
                          Test Screen Name Most Valuable Participant

                          George's suggestion is a standard technique in programming, and is often called a State Table. I just mention that so, if you choose to follow this up with computer science textbooks, you may have a pointer on where to look. Entire games can often be controlled by state tables.