How do you add a new number to the array that are Math.random() from the range -100 to +100, and then does + or - the random number it got and does the math on the previous number in the array before pushing it to the array?
MY CODE
var dataArr = [];
var newValue = 0;
for (let i = 0; i<151; i++) {newValue += Math.random() * 100 - 100} {
dataArr.push(newValue);
};
Easier explanation
A loop that gives the array 150 numbers.
First number (X) in the array is a random number (-100 to +100).
Next number (N) is (X -100 to +100) = random number from min = -100, max = 100, and makes the math. If minus then it subtracts x with it, if positive, then adds.
Third Number (B) is (N -100 to +100).
dataArr = [X, N, B]. (and it will loop till the array have got 150 numbers in it).
X, N, B is just the first 3 numbers as explanation.
let min = -100;
let max = 100;
var dataArr = [];
var newValue = 0;
for (let i = 0; i<151; i++) {
newValue += Math.floor(Math.random() * (max - min + 1)) + min;
dataArr.push(newValue);
};
edited:
sorry I had the wrong variable, had the solution.
I had myMin instead of min so now it works.
let min = -100;
let max = 100;
var dataArr = [];
var newValue = 0;
for (let i = 0; i<151; i++) {
newValue += Math.floor(Math.random() * (max - min + 1)) + min;
dataArr.push(newValue);
};
How do I refresh the loops(So it does it again) if it reaches (<= -1 or >= 1700)?
I need a if statement or something for this, but don’t know how to make it. Please help.
It does Math.random() with the range -100 to 100, but now when I tested it out in the browser, my line graph it goes under my canvas so the line graph gets invincible if it’s under -1, so it needs to get restricted on getting to that number or under.