# Undefined JS variable

I have this code that is supposed to create a list of 100 random numbers between 0-1, then reshuffle those numbers so that all the numbers have a mean of 0.75 and an SD of 0.125.

``````randomList = function(n, a, b) {
list = [],
i;
for (i = 0; i < n; i++) {
list[i] = Math.random() * (b - a) + a;
}
return list;
}

descriptives = function(list) {
mean,
sd,
i,
len = list.length,
sum,
a = Infinity,
b = -a;
for (sum = i = 0; i < len; i++) {
sum += list[i];
a = Math.min(a, list[i]);
b = Math.max(b, list[i]);
}
mean = sum / len;
for (sum = i = 0; i < len; i++) {
sum += (list[i] - mean) * (list[i] - mean);
}
sd = Math.sqrt(sum / (len - 1));
return {
mean: mean,
sd: sd,
range: [a, b]
};
}

forceDescriptives = function(list, mean, sd) {
oldDescriptives = descriptives(list),
oldMean = oldDescriptives.mean,
oldSD = oldDescriptives.sd,
newList = [],
len = list.length,
i;
for (i = 0; i < len; i++) {
newList[i] = sd * (list[i] - oldMean) / oldSD + mean;
}
return newList;
}
``````

When I run this online, Pavlovia says i is not defined.

What do I need to do?

Why do you have i by itself in several lines?

To be completely honest, I’m using JS code that I found on stackoverflow. I’m don’t know a great deal about JS.

I would strongly recommend that you read my crib sheet (pinned post) which is focuses on making as much use of the Auto translate feature as possible.

Your randomList function looks like it would work in code_JS but I’d expect list = []; instead of list=[], i;

However, I don’t know enough JavaScript to be able to say how to fix your code.