1

I am programming an online experiment using jspsych/javascript. The experiment consists of 100 blocks and after each block, the block_number gets updated by +1. There are two conditions. On 75 blocks, I want the program to show an image, and on 25 blocks, I want the program to show a letter. First, I generated 25 random numbers (that will serve as 25 block numbers), on which the letter should be shown. My problem is to tell the program to use these 25 numbers as block_number for the letter condition, but use the other block numbers as block_number for the image condition. Maybe I need to store the 25 numbers in a different format, that is, they are not stored as an array? Also in the if statement, how to "connect" block_number to these 25 numbers? Please see my code below. Thank you so much for your help, I really appreciate it!

// initialize block_number at the very beginning, it gets updated +1 after the for loop:

var block_number = 0;

// there are 100 blocks in total, and on 25 of them (picked randomly at the very beginning), a letter instead of an image should be shown:

var random_numbers = Array.from(Array(95), (x, index) => index + 5);
random_numbers = jsPsych.randomization.shuffle(random_numbers);
var block_keys = random_numbers.filter((random_numbers,idx) => idx < 25);
block_keys.sort();

// in the if statement, I want to show letters only when block_number is equal to block_keys, otherwise, images should be shown:

  if (block_number == block_keys) {
    show letter
  } else {
    show image
  }
Christina
  • 13
  • 2
  • I'm struggling a little to understand the question, but is the end goal to test whether your `block_number` is contained in your `block_keys` array? – DBS Aug 08 '19 at 08:24
  • yes, that is the goal. whenever block_number is equal to a number in block_keys, the program should do something else – Christina Aug 08 '19 at 08:47
  • In that case, you should simply be able to do something like `block_keys.includes(block_number)` [JS includes documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) – DBS Aug 08 '19 at 09:54
  • Thank you for your help! There is no error message, but unfortunately it doesn't work as intended either. – Christina Aug 08 '19 at 13:06
  • You've included it like this: `if (block_keys.includes(block_number)) { ...`? I'm not sure how that could fail without erroring, the `if` has to go one way or the other. Might be worth getting a small working example into the question so it's easier to diagnose. – DBS Aug 08 '19 at 13:31
  • Thank you for your response! Yes, it says if (block_keys.includes(block_number)) {... I changed the conditions to check what is going on in the if-else statement, and the program always does what is written in the else part. – Christina Aug 08 '19 at 14:14
  • If you `console.log()` out the `block_keys` and `block_number` variables, what do you get? – DBS Aug 08 '19 at 14:29
  • I get a simple number like 4 for block_number and it is updated +1 on every iteration. For block_keys, on every iteration, it says (25) [7, 10, 11, 12, 15, 19, 33, 34, 38, 41, 44, 45, 47, 54, 57, 71, 72, 74, 80, 82, 85, 86, 89, 93, 97] – Christina Aug 08 '19 at 14:40
  • I'm afraid I'm out of ideas, your data and code seem like they should work to me. Perhaps I'm fundamentally misunderstanding the issue, but as far as I can tell, it should be working: https://jsfiddle.net/xvgzj20s/ – DBS Aug 08 '19 at 14:59
  • it's working now! I moved the block_number updating part right before the if part. I really appreciate your help and your effort!!! Thank you very much!!! – Christina Aug 08 '19 at 15:13
  • No problem, happy to help :) – DBS Aug 08 '19 at 15:14

0 Answers0