These are a collection of various common and frequently required business use cases that can be solved with Rival Script.
Calculate numeric age from birth year
age_year = gDBL("Q6_Age_Year").Value
-- Q6_Age_Year is the open-end numeric question
age_num = getDateTimeDifference(getCurrentDateTime(), age_year .. "-01-01T12:00:00.000Z", "years")
age_short = floor(age_num)
return age_short
Add Prefix 0 to 4 digit US Zipcode
zip = "" .. gDBL("Q5_Zipcode").Value
-- Q5_Zipcode is the numeric zipcode question
if #zip < 5 then
zip = "0" .. zip
end
return zip
Calculate age generation
age_num = gDBL("Q6_Age_Year").Value
-- Q6_Age_Year is Participants birth year
if (age_num >= 1997) then
return {4}
elseif (age_num >= 1979) then
return {3}
elseif (age_num >= 1964) then
return {2}
elseif (age_num >= 1959) then
return {1}
end
Calculate age from day, month, year
age_year = gDBL("Q1_Age_Year").Value
age_month = gDBL("Q1_Age_Month").Value
age_day = gDBL("Q1_Age_Day").Value
--age_year, age_month, age_day are open-end numeric questions
if age_month < 10 then
age_month = "0" .. age_month
end
if age_day < 10 then
age_day = "0" .. age_day
end
date_str = age_year .. "-" .. age_month .. "-" .. age_day .. "T12:00:00.000Z"
age_num = getDateTimeDifference(getCurrentDateTime(), date_str, "years")
age_short = floor(age_num)
return age_short
Age rollup from numeric age
age_num = gDBL("HQ1_Age_Num").Value
if (age_num >= 55) then
return {5}
elseif (age_num >= 45) then
return {4}
elseif (age_num >= 35) then
return {3}
elseif (age_num >= 25) then
return {2}
elseif (age_num >= 18) then
return {1}
end
Rollup of 2 variables - Country Age Rollup example
-- just need to replace the question names for a double nested flag
outer = gDBL("HQ2_Country")
inner = gDBL("HQ1_Age_Rollup")
outer_code = outer.Value.Code
outer_len = size(outer.Choices)
inner_code = inner.Value.Code
inner_len = size(inner.Choices)
result = (((outer_code - 1) * inner_len) + inner_code)
return {result}
Rollup of 3 variables - Region Gender Age Rollup
-- just need to replace the question names for a triple nested flag
outer = gDBL("HID_Country")
middle = gDBL("HID_Gender")
inner = gDBL("HID_Age")
outer_code = outer.Value.Code
outer_len = size(outer.Choices)
middle_code = middle.Value.Code
middle_len = size(middle.Choices)
inner_code = inner.Value.Code
inner_len = size(inner.Choices)
result = (((outer_code - 1) * middle_len * inner_len) + ((middle_code - 1) * inner_len) + inner_code)
-- double nested flag would just be simplified to = (((outer_code - 1) * inner_len) + inner_code)
return {result}
Calculate Overall Satisfaction for a Travel Agency
We will go with our fictional chat that we have been following throughout this document.
There is total seven Single Choice survey questions:
Label:
"Q1_Concept_1"
Text: How satisfied you are with your vacation in terms of amenities at your stay?
Label:
"Q2_Concept_2"
Text: How satisfied you are with your vacation in terms of food?
Label:
"Q3_Concept_3"
Text: How satisfied you are with your vacation in terms of activities?
Label:
"Q4_Concept_4"
Text: How satisfied you are with your vacation in terms of cleanliness?
Label:
"Q5_Concept_5"
Text: How satisfied you are with your vacation in terms of staff?
Label:
"Q6_Concept_6"
Text: How satisfied you are with your vacation in terms of location?
Label:
"Q7_Concept_7"
Text: How satisfied you are with your vacation in terms of value for money
Each of these have the same choices with a code attached to them which in this example we are treating as a score.
Choice
1
: Bad, Code (Score):1
Choice
2
: Ok, Code (Score):2
Choice
3
: Good, Code (Score):3
Choice
4
: Excellent, Code (Score):4
Choice
5
: Outstanding, Code (Score):5
Minimum score: 7
Max score: 35
Here is a script that can be utilized to calculate the score:
Assume the following responses for each of the questions
"Q1_Concept_1"
=> Ok (Code:2
)"Q2_Concept_2"
=> Good (Code:3
)"Q3_Concept_3"
=> Ok (Code:2
)"Q4_Concept_4"
=> Excellent (Code:4
)"Q5_Concept_5"
=> Excellent (Code:4
)"Q6_Concept_6"
=> Outstanding (Code:5
)"Q7_Concept_7"
=> Good (Code:3
)
Overall Score should be 35
local overallSatisfactionScore = 0
for i = 1, 7 do
question = getDatapointByLabel("Q1_Concept_"..i)
if question.IsSet == true then
overallSatisfactionScore = overallSatisfactionScore + question.Value.Code
end
end
log("Overall Satisfaction Score: ", overallSatisfactionScore)
return overallSatisfactionScore