Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

[Q] How to build Javascript array from survey answers

More
6 years 2 months ago #163011 by w14a
Hi, have got stuck..

Survey answers - bunch of date fields (string format, I suppose, isn't it?).
In order to find the latest date stated within the answers, at the end of the survey I need to build the array from the values of particular questions and compare the values against each other.
Javascript hypothetical fragments might look like
var bigarray = ["{Q001}", "{Q002}", "{Q00N}"];
var len=bigarray.length;
oneoftehdateis = bigarray[len];
anotherdateis= bigarray[len-1];
and so on.
My question is how to correctly build the array in Javascript and compare those values taking into account that the dates are compared, not a kind of strings. Thanks a lot!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 2 months ago #163080 by tpartner
Try this:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
    var dates = ['{Q1}', '{Q2}', '{Q3}'];
    dates.sort();
 
    var firstDate =  dates[0];
    var lastDate = dates[dates.length-1];
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
6 years 2 months ago - 6 years 2 months ago #163084 by w14a
Thank you very much for reaction and kind response. I believe, my errors are connected with complexity of the initial task that is about to compare dates against some constants and counting them also, sorting and different formats when compared... Simply, I did not have fast scripting skill, so your help is appreciate very much ... I did not describe the whole intention initially. The Idea was to get several evaluation angles from single answer to the single question.
My scenario:
The dates in myarray[] always mean several things, so I need to interpret them by some logic: 1) Dates in a past incl. today (all should be first compared against Today, of course) should total count as numberofpastdates, 2) future dates should be total count as numberoffuturedates, plus, 3) the latest future date should be assigned to the new variable, 4) if no future dates are provided, the var x should get value false, 5) Whole empty array cells (date is not provided by respondent) should be total count as nodatesprovidednumber and, finally, 6) the particular dates, say equal to YYYY-MM-DD, should be total count as particulardatesnumber.
I have used while() and case(true) statements and expressions, but finally messed up with the script and lost in space.:( Depending on my next 3 days success, our ~3000 respondents will or will not get valuable statistical graph on their last page; btw, thanks for google graph libraries... Here is not a profitable project, only my afterhours intention to have the survey better shaped than standard "acceptable" level.
Thanks again for your kind support and invaluable ideas.
Last edit: 6 years 2 months ago by w14a.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose