Answered

How to compare two variables for equality

  • 26 January 2022
  • 13 replies
  • 1252 views

I want to compare @score variable with another variable for equality instead of 0. Can it be done?

icon

Best answer by john.desborough 26 January 2022, 12:50

View original

13 replies

Userlevel 7
Badge +6

@zmahmud - welcome to the community

here’s a ‘way’ to do this check for equality

  • create a third variable to handle the evaluation - i will call it v_equal
  • create a text variable called v_is_equal and type in ‘no’ as the default value
  • logic rules:
    • if score is greater than or equal to 0 then add score to v_equal
    • if v_other is greater than or equal 0 then subtract v_other from v_equal
    • if v_equal = 0 replace v_is_equal with ‘yes’

if the result of the subtraction is = 0 then the two values are equal. 

 

 

Userlevel 7
Badge +5

Hi @zmahmud Were you able to set up the comparisons? Let us know how it went!

Userlevel 2
Badge +2

Hi @john.desborough - first of all I so appreciate your advice in this forum! I have been searching many questions for an answer to my challenge and your answers have helped me tremendously.

In my typeform assessment, I’m trying to validate all of the multiple choice answers by comparing a few of them against each other. I’m taking 10 of the questions and making sure the scored answers are within 1 point, essentially for validity.

This is how the answers are scored (some are scored exactly this way, while some are reverse-scored):
 



I need to compare 5 different questions directly against one other question, (so 10 questions in all, in 1:1 comparisons). so I’m thinking that your technique in this thread is my best bet! 

My questions are these:

  1. what logic rules can I use if I don’t need them to match exactly, but just be within 1 point?

 

  1. Then I need a formula to determine if 8/10 of those pairs are within 1 point. Is this doable within Typeform?


I also don’t want it to show up within the Typeform itself, but just create a variable to be sent to Zapier, so I don’t need a different ending screen or anything.

 

Thank you for your service!

Userlevel 7
Badge +6

@SmallBiz Sidekick - thanks for the kinds words.. tis appreciated. 

can  you clarify the following for the seven other voices in my head who will work on this while i sleep: 

  • your image shows using @score for a question - if you are doing that and adding or subtracting the values from @score (a default variable) then you are dealing with a running total and comparing q vs q is going to be difficult. is this the case?
  • if i was going to compare say q4 against q10 and both had a range of possible points/score of 5-1 based on the option chosen, i would use logic something along this line (at 10 and about 5 hours away from caffeine.. lol .. i might be a little out of what but) 
    • if q4 is greater than 0 then add q4 to v_eval_4_and_10 
    • if q10 is greater than 0 then subtract q10 from v_eval_4_and_10
    • if v_eval_4_and_10 GTE -1 AND v_eval_4_and_10 LTE 1 replace variable ‘close_enough_4_10’ with yes
    • if ‘close_enough_4_10’ is yes then add 1 to variable ‘counter’ 
    •  
    • repeat for the other question pairs and then once you have done all ten, 
      • if variable ‘counter’ GTE 8 then replace variable ‘success’ with yes  (set the variable default to no) 
      • so you could send the count or the text variable to zapier methinks at this point

to me that would compare the question responses to as follows:

  • if q4 = 3 and q10 = 4 then the result would be -1 (3-4=-1)
  • if q4 = 4 and q10 =3 then the result would be 1 
  • if they were equal the result would 0 ..

all three of those conditions seem to be the match for your logic ie a 1 and 5 would be either 4 or -4 and outside the range .. 

 

hopefully that makes sense - it does mean you need some ‘scratch pad’ variables to do the calculations (don’t use @score!!) 

hopefully that makes sense.. 

cheers

 

des

Userlevel 2
Badge +2

@john.desborough Yes you are correct I was going to add a different variable for each of the 10 questions I was going to use - not use @score! 

(However, I am using @score for the main score, separate from this “equality” test within the main assessment”)

 

I will give your logic a whirl and report back. Thank you!! Seriously you are a logic & scoring whiz.

Userlevel 2
Badge +2

@john.desborough Ok I finally had time to circle back to this and had a question for you (two qs actually)…

1- The first step you laid out is inputting:

  • if q4 is greater than 0 then add q4 to v_eval_4_and_10 
  • if q10 is greater than 0 then subtract q10 from v_eval_4_and_10

But they will both always be greater than 0 because they are both required questions, and the lowest option is 1... What do you recommend in that case?

2- The ‘close_enough_4_10’ variable would be a text variable too yes? (in order to replace it with “yes”?) What should the starting variable be?

Thank you thank you! This is so above my capabilities right now, I am learning a ton from you.

Userlevel 7
Badge +6

@SmallBiz Sidekick - it’s a foible of the typeform logic rules environment … you need to start with something like that. if greater than or equal to 1 then change the logic to 1

 

you can set the close enough variable (text) to a default of no and replace it with yes in the logic. 

 

 

Userlevel 2
Badge +2

@john.desborough This works perfectly. Thank you so much! Its amazing 😄

Userlevel 1

 For anyone who thinks @john.desborough’s solution is too complicated, I came up with a solution that makes more sense to me.

Here is how it works:
If I have variables a, b, and c that each store a value and I want to show the user a slide based on whichever variable is the highest this is the simple logic you need to use.

if (initiating event = true)
a - b

if (a  > 0)
a + b
if (a > 0)
a - c
if (a > 0)
go to slide for when a is the greatest variable (if this is not true it will continue on to compare b to all other variables, which in this case is c because we already know a is smaller than b)

if (initiating event = true)

b - c

if (b > 0)
go to slide for when b is the greatest variable

if (initiating event = true)
go to slide for when c is the greatest variable 


This solution works because Typeform is not asynchronous. If you understand the basic logic pattern, this can be adapted to compare any number of variables and is very easy to set up. I have used this to compare over 15 variables. I hope this helps!
 

Userlevel 7
Badge +6

@Haven321 - you can make it even simpler:

  • if A is greater than B and A is greater than C then go to ending_A
  • if B is greater than A and B is greater than C then go to ending_B
  • if C is greater than A and Cis greater than B then go to ending_C

That will get you to an ending if you have a unique high score possible at all times … it gets a little more complicated when you have the possibility of one or more categories being tied with the high score

you can throw in some ‘tie breaker logic too: 

  • if A = B and A =C then go to tied_ABC
  • if A = B and A greater than C go to tied_AB
  • if A= C and A greater than B go to tied_AC
  • if B = C and B greater than A then go to tied_BC

On each of those tie breaker pages you have the user make a choice and you get to the/an appropriate ending 

des

Userlevel 3

Hi @Haven321 

That sounds like an interesting solution, as currently we do not offer a specific logic feature to directly compare variables with each other. Would you mind sharing the URL of the form where you implemented this set up and let us in support check your form’s logic? We’d be interested to see if we can make a section on the Help centre for this, as other users have been asking as well. 

Userlevel 1

Hey @Rai, I would love to help. I don’t wan’t to share the form here, but would do so over email. You can reach me at havendewart@gmail.com.

 

Userlevel 3

hi @Haven321 thanks, I have sent you an email a few moments ago! Much appreciated!

Reply