Creating one time password (OTP) and view on SL3

Hi there,

I have went online to download the following code for creating OTP

function generateOTP()

{
var digits = '0123456789abcdefghijklmnopqrstuvwxyz';
var otpLength = 6;
var otp = '';
for(let i=1; i<=otpLength; i++)

{

var index = Math.floor(Math.random()*(digits.length));
otp = otp + digits[index];

}

return otp;

 

I'm not very good at javascripts….I want to view the OTP on the slide with %NewOTP%...but I  can't seem to be able to. Below is what I added

 

var player = GetPlayer();
var NewOTP = player.GetVar("otp');

 

 

4 Replies
Matthew Bibby
irven Lim
var player = GetPlayer();
var NewOTP = player.GetVar("otp');

The above means that you are getting a variable called otp from Storyline and passing it to a JavaScript variable called NewOTP.

I think what you are trying to do is:

var player = GetPlayer();
player.SetVar("NewOTP",otp);

This will take the value stored in otp and pass it to a Storyline variable called NewOTP so you can use it in Storyline.

Matthew Bibby

The issue is with your other code Irven.

Because if you add this to your project, the value will be displayed:

var otp = '23452345'; //this will display in SL
var player = GetPlayer();
player.SetVar("NewOTP",otp);

Try using this instead:

var passwordChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#@!%&()/";
var otp = Array(6).fill(passwordChars).map(function(x) {
return x[Math.floor(Math.random() * x.length)]
}).join('');
var player = GetPlayer();
player.SetVar("NewOTP",otp);

It should do the trick.