Jenith Posted May 21, 2019 Share Posted May 21, 2019 Hi, I'm a Psychology student and newbie to javascript. I am trying to create a behavioural task on qualtrics using javascript. The game works and I am able to provide the participant with feedback at the end of each round (there are 10 atm). What I would like to know is whether there is a way for the following six sentences to be presented one at a time in a consecutive order? I can get the task to present one of the six at random, however, when I remove Math.random from the code, all six sentences are displayed as one chunk. var feedback_array1= [ "<p>Congratulations you won the game. </ p> <p>The other player picked A</p>", "<p>Congratulations you won the game. </ p> <p>The other player picked B</p>", "<p>Congratulations you won the game. </ p> <p>The other player picked C</p>", "<p>You lost! </p><p>The other player picked A</p>", "<p>You lost! </p><p>The other player picked B</p>", "<p>You lost! </p><p>The other player picked C</p>", ]; var msg_feedback1 = function(){ return feedback_array1 [Math.floor(Math.random()*feedback_array1.length)] ; }; An alternative method would be to save the order of the randomized feedback, through I have had much luck devising that either! Any help would be greatly appreciated. Many thanks in advance! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 21, 2019 Share Posted May 21, 2019 You can loop through the array, or you can use Array.join to combine them into one string. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 22, 2019 Share Posted May 22, 2019 (edited) Look into setTimeout() this will call a function that will increment a index ref of each array value to a specific time in milliseconds. You then append the values to show all values one at a time OR innerHTML to show single value of each overwriting previous value, all within a parent container with a specific id ref depending on requirements. Edited May 22, 2019 by dsonesuk Link to comment Share on other sites More sharing options...
Jenith Posted May 24, 2019 Author Share Posted May 24, 2019 Hi, many thanks for your assistance. I have tried to create a loop but I am having a similar problem - all six sentences are presented as one. example of code I have tried: for (i = 0; i < feedback.array1.length; i++){ msg_feedback1 += feedback_array1; I've had a look at setTimout() and I'm not sure a time sensitivity option will work either. In the game, the participant will press a button and then be shown feedback. They press the button again and will be shown a different feedback sentence. I can get this to work if the feedback is random - as per the code stated above. But this information is not stored. Can anyone recommend anything else I might try?? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 24, 2019 Share Posted May 24, 2019 Keep a counter, i in the code above, as a global variable. Every time they press the button, display feedback_array[i] then increment i to point to the next item for when they press the button again. Link to comment Share on other sites More sharing options...
JMRKER Posted May 29, 2019 Share Posted May 29, 2019 I'm not exactly sure of the original concept, but you can try to use this if it is of any help. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/> <title> HTML5 Test Page </title> <!-- For: http://w3schools.invisionzone.com/topic/59173-how-to-order-sentences-in-a-non-random-order/ --> </head> <body> <button id="inSequence">Display in sequence</button> <button id="inRandom">Random Display</button> <p> <div id="demo"></div> <script> var feedback_array1 = [ "<p>Congratulations you won the game. </ p> <p>The other player picked A</p>", "<p>Congratulations you won the game. </ p> <p>The other player picked B</p>", "<p>Congratulations you won the game. </ p> <p>The other player picked C</p>", "<p>You lost! </p><p>The other player picked A</p>", "<p>You lost! </p><p>The other player picked B</p>", "<p>You lost! </p><p>The other player picked C</p>", ]; var msg_feedback1 = function(){ return feedback_array1 [Math.floor(Math.random()*feedback_array1.length)] ; }; var order = 0; // global variable function init() { document.getElementById('inSequence').addEventListener('click', function() { document.getElementById('demo').innerHTML = feedback_array1[order]; var len = feedback_array1.length; if (order < len) { order++; if (order >= len) { order = 0; } } } ) document.getElementById('inRandom').addEventListener('click', function() { document.getElementById('demo').innerHTML = msg_feedback1(); } ) } init(); </script> </body> </html> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now