Score:0

Why is this if statement not working correctly in Webform computed twig?

ph flag

I'm using this in a computed_twig element and it's fully working when I test it in this fiddle but only works correctly for value "0" while gives "high" for everything else (e.g. 0.1, 0.4, 0.5, etc) when in my webform!
q# are values of radio input type elements. I tried several variations all to no avail! Thanks

Edit: updated my question and added a minimal sample of my webform. I didn't post this at the twig stack because it's working on fiddle and only when in my Drupal Webform it's malfunctioning. I tried with and without the number_format filter kindly suggested by @cilefen but still no change in the result. I'd exhausted my search online and read and saw examples on official Twig and other pages and still couldn't figure out what I'm doing wrong! I don't know much about twig apart from what I learn on the go to meet my special need at the time.
Note: I changed "TsC" in my previous post version to TCount to be more meaningful. Also, it's on a shared host with PHP 7.4 still.
Thanks to everyone who commented so far and I wish that whoever downvoted me straight away after posting explained why so I could learn for future posts.

qp1:
  '#type': wizard_page
  '#title': 'Part I'
  '#open': true
  ps1:
    '#type': details
    '#title': title...
    '#required': true
    '#attributes':
      class:
        - qp1_1
    q1:
      '#type': radios
      '#title': q1.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q2:
      '#type': radios
      '#title': q2....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q3:
      '#type': radios
      '#title': q3.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q4:
      '#type': radios
      '#title': q4.......
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q5:
      '#type': radios
      '#title': q5........
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q6:
      '#type': radios
      '#title': q6.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q7:
      '#type': radios
      '#title': q7....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q8:
      '#type': radios
      '#title': q8.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q9:
      '#type': radios
      '#title': q9.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q10:
      '#type': radios
      '#title': q10.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q11:
      '#type': radios
      '#title': q11....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q12:
      '#type': radios
      '#title': q12.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q13:
      '#type': radios
      '#title': q13.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q14:
      '#type': radios
      '#title': q14.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
      '#required': true
    q15:
      '#type': radios
      '#title': q15......
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q16:
      '#type': radios
      '#title': q16.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    qs_count:
      '#type': computed_twig
      '#title': 'Total Count'
      '#wrapper_attributes':
        class:
          - score_qp_1
      '#template': 'Total Count: {% set TCount = (data.q1 + data.q2 +  data.q3 +  data.q4 +  data.q5 +  data.q6 +  data.q7 +  data.q8 +  data.q9 +  data.q10 +  data.q11 +  data.q12 +  data.q13 +  data.q14 +  data.q15 +  data.q16)|number_format(0) %} {{ TCount }} <br /> Ratio: {{ TCount / 16 }} <br /> {% if TCount == 0 %} Great {% elseif TCount > 0 and TCount < 0.5 %} Low {% elseif TCount >= 0.5 and TCount < 0.7 %} Moderate {% elseif TCount >= 0.7 %} High {% endif %}'
      '#ajax': true
Adam888 avatar
ph flag
I'd love to know why my question was down-voted within almost a minute (? less) of posting and no explanation whatsoever ! Is this an automated badly programmed AI job :-? It couldn't be more frustrating and off-putting to new comers and I noticed it's the worst on this Drupal Stack !
cn flag
I haven't voted, but could you perhaps highlight how your question relates to Drupal, and isn't just about Twig? It seems like `TsC` isn't what you expect it to be, which you could confirm by simply printing it?
id flag
PHP 8 is stricter about types, and that can affect these calculations in Twig. Many bug issues were opened for Webform about this but all were actually programming support requests. We don’t actually know what the objects in your code are and so currently this is unanswerable. And I agree this looks like a Twig programming question.
id flag
I think they may be strings, right?
id flag
Some have had success with the `|number_format` filter on string operands which are to be used in computations.
Adam888 avatar
ph flag
@clive thank you. TsC (TCount now) seems to be printing correctly. I updated my question with more info and form sample
Adam888 avatar
ph flag
@cilefen, thanks so much, please see my updated question with more info and complete form sample
Score:0
ph flag

I am going to answer myself and it seems the culprit was I was setting the conditionals based on TCount while it should be on the ratio (TCount / 16) instead. My bad! Sometimes the culprit would be too silly to miss (and ? notice) and right in front of us but we fail to see despite the many revisions and even different people. Thanks so much everyone. My template now is:

'Total Count: {% set TCount = (data.q1 + data.q2 +  data.q3 +  data.q4 +  data.q5 +  data.q6 +  data.q7 +  data.q8 +  data.q9 +  data.q10 +  data.q11 +  data.q12 +  data.q13 +  data.q14 +  data.q15 +  data.q16)|number_format %} {{ TCount }} <br /> {% set tRatio = TCount / 16 %} Ratio: {{ tRatio }} <br /> {% if tRatio >= 0.7 %} high {% elseif tRatio > 0 and tRatio <= 0.5 %} low {% elseif tRatio > 0.5 %} moderate {% else %} great {% endif %}'
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.