# Javascript code not working on Pavlovia

**URL:** <https://discourse.psychopy.org/t/javascript-code-not-working-on-pavlovia/10795>\
**Category:** Builder\
**Created:** [January 31, 2020, 9:41pm UTC](https://discourse.psychopy.org/t/javascript-code-not-working-on-pavlovia/10795 "2020-01-31T21:41:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smoha005](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@smoha005](https://discourse.psychopy.org/u/smoha005)\
**Post date:** [January 31, 2020, 9:41pm UTC](https://discourse.psychopy.org/t/javascript-code-not-working-on-pavlovia/10795/1 "2020-01-31T21:41:33Z")

</div>

Hello,

I’m trying to get this code working on pavlovia, but apparently I need to change it into a JS code first.  
In this routine, I’ve randomised the sound between mute 0 and sound 1. I have done this on the builder view using the custom code (inserting python commands to the experiment)  
the code I had converted to JS as well so I can upload the experiment on Pavlovia is not working - stuck on initailising

The experiment works perfectly on locally but not online

example, CODE 3 - sound randomisation  
soundFlag = randint(1,10)  
if soundFlag \< 6:  
soundFlag = 0  
else:  
soundFlag = 1

sound\_5.setVolume(soundFlag, log=False)

And the JS equivalent

function getRandInt(min, max) {  
min = Math.ceil(min);  
max = Math.floor(max);  
return Math.floor(Math.random() \* (max - min + 1)) + min;  
}

soundFlag=getRandInt(1,10);  
if(soundFlag\<6){  
soundFlag=0;  
}  
else{  
soundFlag=1;  
}

Wait Time  
waitTime=getRandInt(2,8);  
waitTime=waitTime/10;

But it is not working  
Link is [https://run.pavlovia.org/smoha005/sfm\_tffm/html/](https://run.pavlovia.org/smoha005/sfm_tffm/html/)

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [February 2, 2020, 8:35am UTC](https://discourse.psychopy.org/t/javascript-code-not-working-on-pavlovia/10795/2 "2020-02-02T08:35:40Z")

</div>

Hi @smoha005. please [surround code with backticks](https://discourse.psychopy.org/t/please-surround-python-code-with-backticks/1775) for readability.

I think this is just the typo, probably uncommented code `Wait Time`.

---

<div class="post-metadata">

**Author:** ![smoha005](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@smoha005](https://discourse.psychopy.org/u/smoha005)\
**Post date:** [February 4, 2020, 8:46pm UTC](https://discourse.psychopy.org/t/javascript-code-not-working-on-pavlovia/10795/4 "2020-02-04T20:46:02Z")

</div>

I would like auditory beeps (sound) to occur randomly in half of the trials in the experiment. I have converted the python commands to JS however, I cannot hear any sounds on any of the trials when uploaded on Pavlovia.

The python commands:

````auto
if soundFlag < 6:
    soundFlag = 0

else:
    soundFlag = 1

sound_5.setVolume(soundFlag, log=False)```

```/////////////Function to generate random Numbers////////////
function getRandInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

soundFlag=getRandInt(1,10);
if(soundFlag<6){
soundFlag=0;
}
else{
soundFlag=1;
}```
````
