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


It goes under the canvas, I need to make some function or something to prevent it to do that.

I want it to only get the ones that you can see 100%

Their is already a scaling function/loop, but it doesn’t cover it when it reaches negative values I guess.

I think I need to make a new topic for this one, because I would guess that people think this topic is already solved, so they don’t take a look at it.

You do not need to create a duplicate topic.

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);
};

I wonder how to make it so it gets restricted from getting a number under 0.
I need the range with -100, but some kind of system that prevents it to get -1 or less.

If it’s at 100 for example then -100 should work.

If that doesn’t work to make, then I need it to refresh the page or restart the for loop till it gets the correct data.

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;
  while (dataArr[i] <= -1) {
    dataArr[i]++;
  }
  dataArr.push(newValue);
};

I tried a while loop, but it’s still not solving my problem.

Here is an example of how it looks when it reaches -1


I added numbers manually so it’s easier to see.

Hey @amejl172 , I don’t really get your goal.
Seems to me that you are specifically generating number in the range from -100 to 100.

Now you want numbers that goes from 0 to 100?
If so why not simply updating your min?

1 Like

I want it to be able to do minus

Seems like your statements are conflicting.
So you want it to go up to -100 but not producing anything below 0?

How should that work? Skip number so the probability of a positive number stays the same?
Seems rather confusing to me.

1 Like

Lets say it gets the number -1 or less, then I want some kind of while loop that changes it to 0.

I tried this but it doesn’t work, why?

The only reason to do minus is if it doesn’t over do it on a plus so it turns to a negative number in the array.

The increment ++ operator doesn’t work that way on Arrays, and even if it did it would just increment the value, not making it positive.

What you want is called in math the Absolute value, which conveniently has a method in JS: Math.abs

Math.abs(-20) // 20
Math.abs(20) //20
Math.abs(-0) // 0
1 Like

Why is this not working?

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;
  if (dataArr[i] <= -1 ) {
    Math.abs(dataArr[i]);
  }
  dataArr.push(newValue);
};


I still get negative values in the array?

  if (dataArr[i] <= -1 ) {
    Math.abs(dataArr[i]);
  }

How exactly can this work without an assignment?
At this point I will let you do your own research on basic JS as I feel like if I give you away the answer will do a disservice.

There’s even an easier solution: why not simply make so the number is always positive before adding it to the array? Instead of checking and modifying it later?

1 Like

I need to find the correct index of the negative numbers in the array, how do I do that?

I’m using freeCodeCamp to get the answers, it’s part of google.
When you google stuff to find solution, freeCodeCamp exists on google.

freeCodeCamp primary goal is to be a learning platform, not an answer board.
We are always happy to help with helping with general topics, but the main focus is always having a good learning experience.

Often time a direct answer is not part of a developer’s growth.

That said I still think you should try to come up with a solution yourself as this is “common routine” and you need to learn to do it yourself if you aim to grow as a developer.

Here below wrapped in spoiler if you really want to copy:

1 - Have your number always positive before pushing them:

newValue += Math.abs(Math.floor(Math.random() * (max - min + 1)) + min);

or
2 - Map the array later

for (let i = 0; i<151; i++) {
 // same implementation as you have.
};
// map it later
const onlyPositive = dataArr.map(n => Math.abs(n));
1 Like

It’s the same as not having -100 in the range.

This is why I need negative
I don’t want it to go one direction.

This is with -100 in min
Also I have tried with just Math.random and giving it a random number that is positive, but then the lines movement becomes to long and, it’s hard to use.

I need some formula that stops giving it negative when it’s at zero or when it’s higher negative value than the positive value one.

I still don’t get it but:

you want newValue to go up and down
BUT
the data in the array must all be positive

Is that the condition you want?
If so keep newValue as it is:

 newValue += Math.floor(Math.random() * (max - min + 1)) + min;

but push only positive numbers:

dataArr.push(Math.abs(newValue));
1 Like

I need to push negative values to get it to go down.

Only push negative values if the negative is for example -10 and the positive is 11,

as long it doesn’t becomes a negative value in the array it can do a subtraction with the positive value in the array.

Examples:
dataArray =[1,20,100,120]
-1 to -100 is ok.

dataArray =[1,20,100]
-1 to -100 is ok.

dataArray =[1,20, 57]
-58 to -100 is not ok but -1 to 57 is ok.

dataArray =[1,20, 57, 70]
-71 to -100 is not ok but -1 to 70 is ok.

dataArray =[1]
Only -1 is ok.

dataArray =[0]
No negative random numbers allowed.

I guess some specific examples could help with understanding the goal here. By that I mean kind of step-by-step walk through, what is starting value of newValue what is then pushed to array and how it changes in array and based on what it changes.

1 Like

I gave newValue a value of 1500 because then it can’t reach -1, but for some reason my computer got very slow from it.

It should be able to reach -1 because 150 * -100 = -15k, but it’s rare it reaches that, or I have some bug that prevents it from reaching that number.

edit: it reached -1.