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.
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.
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);
};
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?
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:
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 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.