Welcome to the Treehouse Community
Looking to learn something new?
The lecture video says we can get equally postive and negative number between -0.5 and 0.5 with Math.random()-.05. When I run the code below, I only see the numbers stored in my array called 'myA' get shuffled around every time I refresh the screen. I thought it was supposed to return positive and negative number between -0.5 to .05. I am not sure of this meaning.
Also, why are we subtracting -0.5 from Math.random, could it be another number?
Math.random() returns a random number between 0 and 1 (0 is inclusive, but 1 is not).
0.5 from the value returned by
Math.random(), you are essentially changing the range of the returned value.
For the smallest value that
Math.random() returns (
0) you will get
-0.5. And for the maximum value (
0.9999...) you will get (
Math.random() - 0.5 will return such numbers, but you are using it inside the compare function of the sort method, so all that the code does is randomly (re)arrange the array.
In the other thread, I asked about what the instructor meant by an array having equal values. What I understand from Jason's post is how to list numbers in an anscending or descending order based on the values we pass into the function parameters, and I am ok with that explantion.
I don't want to compare people's answers or give an opinion about their answers on other posts, but since you are mentionning him I guess I should say I believe your explanation was more along the line of what the video meant by arrays could have equal values.
Anyway, this question is about arrays but very different. Even the code I posted is different. They may share similar concepts but right now I cant see it,
1) I really dont get how: return Math.random()-0.5; (which can return values between -0.5 and 0.49999) causes var myA = [10,44,32, 100, 2]; to shuffle the numbers in the array myA,
2) . Also when I think of numbers between ( -0.5 and 0.49999 ) I dont think os 10, 44, 32.. I think of -0.2, 0.2, .-01, 0.1, so yeah, I cant understand this.
3) Is Math.random() --0.5 a fixed statement can it be Math.random()*.03....
4).... The important part is that it returns randomly positive and negative numbers. How can it return negative numbers with [10,44,32, 100, 2]; All the numbers are positive.
Sorry but, I still can't the connection between -0.5 and 0.4999 and shuffling the numbers inside this array: var myA = [10,44,32, 100, 2]; And equally returning positive and negative numbers????