Jump to content
Jenith

How to order sentences in a non-random order

Recommended Posts

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! 

Share this post


Link to post
Share on other sites

You can loop through the array, or you can use Array.join to combine them into one string.

Share this post


Link to post
Share on other sites

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 by dsonesuk

Share this post


Link to post
Share on other sites

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??

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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>

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...