Title: Converting Nested If AND Excel formula to Javascript
Last modified: June 8, 2022

---

# Converting Nested If AND Excel formula to Javascript

 *  Resolved [minimega](https://wordpress.org/support/users/minimega/)
 * (@minimega)
 * [4 years ago](https://wordpress.org/support/topic/converting-nested-if-and-excel-formula-to-javascript/)
 * Hello!
 * I am building a body shape calculator using the “Calculated Field Forms” Plugin
   and I need help getting the formula to work in Javascript. I have the formula
   working in Excel, but I need help with the conversion.
 * Here is the Excel formula. I’ve tested this and it works:
 * B1 = Bust Measurement
    B2 = Waist Measurement B3 = Hip Measurement B4 = High 
   Hip Measurement
 * Individual formulas:
 * =IF(AND(((B1-B3)<=1),((B3-B1)<3.6),((B1-B2)>=9)), “Hourglass”, “False”)
    =IF(
   AND(((B3-B1)>=3.6),((B3-B1)<10),((B3-B2)>=9),((B4/B2)<1.193)), “Bottom Hourglass”,“
   False”) =IF(AND(((B1-B3)>1),((B1-B3)<10),((B1-B2)>=9)), “Top Hourglass”, “False”)
   =IF(AND(((B3-B1)>2),((B3-B2)>=7),((B4/B2)>=1.193)), “Spoon”, “False”) =IF(AND(((
   B3-B1)>=3.6),((B3-B2)<9)), “Triangle”, “False”) =IF(AND(((B1-B3)>=3.6),((B1-B2)
   <9)), “Inverted Triangle”, “False”) =IF(AND(((B3-B1)<3.6),((B1-B3)<3.6),((B1-
   B2)<10)), “Rectangle”, “False”)
 * Combined into one formula:
 * =IF(AND(((B1-B3)<=1),((B3-B1)<3.6),((B1-B2)>=9)), “Hourglass”, IF(AND(((B3-B1)
   >=3.6),((B3-B1)<10),((B3-B2)>=9),((B4/B2)<1.193)), “Bottom Hourglass”, IF(AND(((
   B1-B3)>1),((B1-B3)<10),((B1-B2)>=9)), “Top Hourglass”, IF(AND(((B3-B1)>2),((B3-
   B2)>=7),((B4/B2)>=1.193)), “Spoon”, IF(AND(((B3-B1)>=3.6),((B3-B2)<9)), “Triangle”,
   IF(AND(((B1-B3)>=3.6),((B1-B2)<9)), “Inverted Triangle”, IF(AND(((B3-B1)<3.6),((
   B1-B3)<3.6),((B1-B2)<10)), “Rectangle”)))))))
 * Here it is converted to reference “fieldname” for the plugin instead of the Excel
   cell reference:
 * =IF(AND(((fieldname1- fieldname3)<=1),(( fieldname3- fieldname1)<3.6),(( fieldname1-
   fieldname2))>=9)), Hourglass, IF(AND(((fieldname3- fieldname1)>=3.6),(( fieldname3-
   fieldname1)<10),(( fieldname3- fieldname2)>=9),(( fieldname5/ fieldname2)<1.193)),
   Bottom Hourglass, IF(AND(((fieldname1- fieldname3)>1),(( fieldname1- fieldname3)
   <10),(( fieldname1- fieldname2)>=9)), Top Hourglass, IF(AND(((fieldname3- fieldname1)
   >2),(( fieldname3- fieldname2)>=7),(( fieldname5/ fieldname2)>=1.193)), Spoon,
   IF(AND(((fieldname3- fieldname1)>=3.6),(( fieldname3- fieldname2)<9)), Triangle,
   IF(AND(((fieldname1- fieldname3)>=3.6),(( fieldname1- fieldname2)<9)), Inverted
   Triangle, IF(AND(((fieldname3- fieldname1)<3.6),(( fieldname1- fieldname3)<3.6),((
   fieldname1- fieldname2)<10)), Rectangle)))))))
 * Based on my javascript research, I need nested if and if else statements. Here’s
   what I have. The most common body shape is on the top and the least common body
   shape is on the bottom. What am I doing wrong and how do I get this to work?
 * if (&& (((fieldname3-fieldname1)<3.6),((fieldname1-fieldname3)<3.6),((fieldname1-
   fieldname2)<10)) {
    fieldname 4 = (‘Rectangle’); } else if (&& (((fieldname3-
   fieldname1)>=3.6),((fieldname3-fieldname1)<10),((fieldname3-fieldname2)>=9),((
   fieldname5/fieldname2)<1.193)) { fieldname4 = (‘Bottom Hourglass’); } else if(&&(((
   fieldname1-fieldname3)>1),((fieldname1-fieldname3)<10),((fieldname1-fieldname2)
   >=9)) { fieldname4 = (‘Top Hourglass’); } else if (&& (((fieldname3-fieldname1)
   >2),((fieldname3-fieldname2)>=7),((fieldname5/fieldname2)>=1.193)) { fieldname
   4 = (‘Spoon’); } else if (&& (((fieldname3-fieldname1)>=3.6),((fieldname3-fieldname2)
   <9)) { fieldname4 = (‘Triangle’); } else if (&& (((fieldname1-fieldname3)>=3.6),((
   fieldname1-fieldname2)<9)) { fieldname4 = (‘Inverted Triangle’); } else (&& (((
   fieldname1-fieldname3)<=1),((fieldname3-fieldname1)<3.6),((fieldname1-fieldname2))
   >=9)) { fieldname4 = (‘Hourglass’); }
 * Thanks!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fconverting-nested-if-and-excel-formula-to-javascript%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Plugin Author [codepeople](https://wordpress.org/support/users/codepeople/)
 * (@codepeople)
 * [4 years ago](https://wordpress.org/support/topic/converting-nested-if-and-excel-formula-to-javascript/#post-15729046)
 * Hello [@minimega](https://wordpress.org/support/users/minimega/)
 * My apologies for the delay in responding to your question. I did not receive 
   the forum notification about your entry.
 * Javascript is a case-sensitive language. Please, do not confuse the `IF` operation
   in the plugin with the Javascript `if` conditional statement.
 * Your equation includes some parser errors. The Excel formula and the equation
   in our plugin are similar, but they have some differences.
 * – You must remove the equal symbol from the beginning.
    – The texts must be enclosed
   between single or double-quotes. – The last IF operation is unnecessary. – And
   please, be careful with the number of parentheses.
 * The correct equation would be:
 * `IF(AND(fieldname1-fieldname3<=1, fieldname3-fieldname1<3.6, fieldname1-fieldname2
   >=9), 'Hourglass', IF(AND(fieldname3-fieldname1>=3.6, fieldname3-fieldname1<10,
   fieldname3-fieldname2>=9, fieldname5/fieldname2<1.193), 'Bottom Hourglass', IF(
   AND(fieldname1-fieldname3>1, fieldname1-fieldname3<10, fieldname1- fieldname2
   >=9), 'Top Hourglass', IF(AND(fieldname3-fieldname1>2, fieldname3-fieldname2>
   =7, fieldname5/fieldname2>=1.193), 'Spoon', IF(AND(fieldname3-fieldname1>=3.6,
   fieldname3-fieldname2<9), 'Triangle', IF(AND(fieldname1-fieldname3>=3.6, fieldname1-
   fieldname2<9), 'Inverted Triangle', 'Rectangle'))))))`
 * Best regards.

Viewing 1 replies (of 1 total)

The topic ‘Converting Nested If AND Excel formula to Javascript’ is closed to new
replies.

 * ![](https://ps.w.org/calculated-fields-form/assets/icon-256x256.jpg?rev=1734377)
 * [Calculated Fields Form](https://wordpress.org/plugins/calculated-fields-form/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/calculated-fields-form/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/calculated-fields-form/)
 * [Active Topics](https://wordpress.org/support/plugin/calculated-fields-form/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/calculated-fields-form/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/calculated-fields-form/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [codepeople](https://wordpress.org/support/users/codepeople/)
 * Last activity: [4 years ago](https://wordpress.org/support/topic/converting-nested-if-and-excel-formula-to-javascript/#post-15729046)
 * Status: resolved