Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Arrays Methods: Part 2

return Math.random()-0.5;

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?

Cheers!!

<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Arrays</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Arrays: Methods Part 1</h2>
    <script>
     var myA = [10,44,32, 100, 2];
    myA.sort(function(a, b)
    {

    return Math.random()-0.5;   
    });

console.log(myA.toString());
    </script>
  </body>
</html>

orange sky, please surround your code snippets with three backticks (```) so that the code is easier to read. Check this link for more info. I've edited your post this time, but in future, please do this yourself.

Hello Dino,

I have just learned how to add the 3 backticks. My code should be a lot easier. :)

3 Answers

Math.random() returns a random number between 0 and 1 (0 is inclusive, but 1 is not).

By subtracting 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 (0.4999...).

The code 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.

Hello Dino,

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????

Thanks!!!

You've already asked about sorting in another thread where Jason Anello gave you a great answer about how the compare function relates to sorting.

Your compare function ignores the parameters that were passed to it and instead returns random numbers between -0.5 and 0.4999.... The important part is that it returns randomly positive and negative numbers. The sort relies on the return value of the compare function. As Jason stated in the other thread, for negative values, a comes before b. For positive values, b comes before a

So, the array is randomly sorted because instead of comparing the numbers, you are randomly returning positive and negative values.