How to convert an array into string and save to an embedded variable. | XM Community
Skip to main content
Solved

How to convert an array into string and save to an embedded variable.

  • January 2, 2024
  • 2 replies
  • 94 views

Forum|alt.badge.img+1

Hello, 

I am designing a study wherein participants are asked to watch a video and press the spacebar every time the people in the video show “bad behaviour”.

I’ve found some code to do this:

let question = this;

/* hide next button */
question.hideNextButton();

/* when the video end, click on next button */
jQuery($('videoPlayer')).on("ended", function() { setTimeout(function () {jQuery('#NextButton').click();},1000); });


document.onkeydown = function(event) {
if (event.which == 32) {
let pressCurnetTime = $('videoPlayer').currentTime /* the time when the subject pressed space */
event.preventDefault();


}

My question is this: how do I make the time an array and record each time the spacebar is pressed?

Following this, how would I convert the array to a string variable and save it to an embedded data variable? 

 

Thank you for your help

Best answer by TomG

The code you are using cold be a lot better, but to answer your question…

Initialize an array variable:

let myArray = [];

Add the times to the array:

myArray.push($('videoPlayer').currentTime);

Join the array into a comma separated string:

let string = myArray.join(",");

 

2 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 6084 replies
  • Answer
  • January 2, 2024

The code you are using cold be a lot better, but to answer your question…

Initialize an array variable:

let myArray = [];

Add the times to the array:

myArray.push($('videoPlayer').currentTime);

Join the array into a comma separated string:

let string = myArray.join(",");

 


Forum|alt.badge.img+1
  • Author
  • 1 reply
  • January 2, 2024

The code you are using cold be a lot better, but to answer your question…

Initialize an array variable:

let myArray = [];

Add the times to the array:

myArray.push($('videoPlayer').currentTime);

Join the array into a comma separated string:

let string = myArray.join(",");

 

Thank you so, so much for your help. I appreciate it. You've saved me a headache! Just additionally, how would I save the array of the times of each space bar press to an embedded data variable so that I can see them easily? Thanks again