0

So basically this is what I'm trying to do. My code runs an AJAX get request on some external JSON data that's stored in arrays (as you can see below).

My code works great.

However I want to apply the same random value on the actual entry number that it's retrieving for both variable sa and ss.

var sa = (index.Of['10']['blah:blah'].label + ' ');
var ss = (index.Of['10']['blah:blah'].label);
var sS = (sa +  ss);

So I can do this with one variable with no issue, in the following code by doing it this way.

var sa = (index.Of[Math.floor(Math.random() * 10) + 1]['blah:blah'].label + ' ');
var ss = (index.Of[Math.floor(Math.random() * 10) + 1]['blah:blah'].label);
var sS = (sa +  ss);

BUT the issue is that I don't know of a way to apply the same math.floor randomization array value to both variables.

I'm stumped.

1
  • If you want Math.floor(Math.random() * 10) to be the same every time, just use let rand = 1 + Math.random() * 10 | 0 and use the rand variable in place of Math.floor…. Commented Nov 17, 2015 at 6:22

2 Answers 2

1

You can randomize a value once, save it to a variable and reuse:

var rand = Math.floor(Math.random() * 10) + 1;

var sa = (index.Of[rand]['blah:blah'].label + ' ');
var ss = (index.Of[rand]['blah:blah'].label);
var sS = (sa +  ss);
Sign up to request clarification or add additional context in comments.

Comments

0
var r = Math.floor(Math.random() * 10) + 1;
var sa = (index.Of[r]['blah:blah'].label + ' ');
var ss = (index.Of[r]['blah:blah'].label);
var sS = (sa +  ss);

?

1 Comment

Can you explain a bit to make your answer more useful for others?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.