Arrays and Math.random(), adding new number + or - previous number

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.

I do not understand. What exactly are you asking for help with in this post? Do you want feedback on your solution?


I think your random number is in the range of -100 to 0.

1 Like

It’s not working.
I’m looking for help solving it.

this is not making a random number between -100 and 100

this is not pushing inside the loop so it’s only last value of newValue being pushed

let’s format it nicely

var dataArr = [];
var newValue = 0;
for (let i = 0; i < 151; i++) {
  newValue += Math.random() * 100 - 100
}
{
  dataArr.push(newValue);
};

not everything you seem to want to do at each iteration is inside the loop

you can also always look at a tool like this to see your code execute:
http://pythontutor.com/javascript.html#mode=edit

my first suggestion is to focus on making a random number in a range

there is a challenge in the curriculum: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range

then once you can do that, what you want to do with it?

2 Likes

It’s not working, I did that challenge and used the code for my Math.random() range,
also I added dataArr.push(newValue); in the loop {}.

What is your updated code?

1 Like

Updated code
||
V

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, image had the solution.
I had myMin instead of min so now it works.

Code

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.

First is there a reason for iterating 151 times

i<151

Second , it seems newValue doesnt reach 1700. it takes from -100 to 100

1 Like

maybe you need a different kind of loop, while could achieve similar result to what you look for i think

1 Like

what should it do? start again the 150 iterations?

1 Like

You’re right. It can only reach 15k.
It’s only -1 that is the problem.

I need it to start again so the array doesn’t get the number -1 or less.

what does it mean “start again”?

1 Like

Like you said either the 150 iterations or if it can’t get to -1 or under in the dataArr.

what do you want to do? scrap everything and redo the array? not accept the single value?

1 Like

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.

You may want to change something, scrapping half your values in first iteration and others going forward can easily become an infinite loop

1 Like

Any examples on how to do that?

I don’t know what you want to do, I can’t give examples

1 Like