This is really confusing

I don’t know how to answer this. Where was this in the course? I just keep throwing in answers til it worked. Where was this presented? I’ve repeated this intro so many times and i can’t do anything like this… where is this taught specifically please? I don’t understand math…failed it until i dropped out.

omplete the code to select a fruit at a random index in the fruits array, and assign it to the randomFruit variable.

const fruits = [‘apple’, ‘orange’, ‘banana’, ‘strawberry’]; const randomIndex = Math.floor( * ); const randomFruit = fruits[randomIndex];

  • Math.pickOne()
  • 3
  • 4
  • Math.random()
  • 5

I believe the use of math.random() is introduced a few times throughout the Introduction to JavaScript chapter? In the first lesson, check at the end when it talks about Built-in Objects. Also in the Magic Eight Ball project in the Conditional section you should’ve used math.floor(math.random * 8)) for your solution.

Anyways, the idea is that math.random() gives you a number between 0 and 1. Since you are trying to pick an item from a list of four, you need to multiply math.random() by 4 so that you get a number between 0 and 4 instead. The last bit is wrapping it with math.floor() to make sure you have an integer, since any index for a list must be a whole number.

Thank you for the reply…I don’t think I did either of those exercises yet, I’ve repeated the JavaScript intro course at least 3 times now because I don’t get the first few lessons still.

How did you know to multiply math.random by 4? Like…how did you know to use that. Integer and index are confusing still, I have less than High School math skills.

I’ve tried the Age in Dog Years and the Kelvin Temperature projects and have no idea where to even begin thinking, i have to read the solution every time but it makes no sense.

  1. List item

math.random() will give us a number between 0 and 1. It could be 0.1, it could be 0.78, whatever. The point is we know it’s between 0-1. In your example, you have a list with 4 things in it:

fruits = [‘apple’,’orange’,’banana’,’strawberry’];

Let’s recall that every item in that list has an index. apple is index 0, orange is index 1, banana index 2 and strawberry is index 3. So! We want a number between 0 and 3 at random, so we get one of the random fruits from the list. Our problem is that math.random() will only give us between 0-1. That’s where the math comes in I’m afraid. The smallest number math.random() is going to give us is 0, so we are good on that end. The max is 1. So if we multiply by 4, that 1 turns into 4.
Now you might be wondering, doesn’t that multiplication change any number that comes out of math.random()? The answer is yes it does. But again we know the smallest is 0 (and zero times anything is still zero), the largest is 1 (and 1 times 4 is 4) and anything in between will fall in that range. It is bound between 0 and 4.
Imagine calling math.random() multiple times:

  • math.random() = 0.2 —> multiply by 4 —> 0.8
  • math.random() = 0.7 —> multiply by 4 —> 2.8
  • math.random() = 0.99 —> multiply by 4 —> 3.96

Like I said, the outcome is bound [0 … 4).

To the question “how did you know to use that”. The simple answer is: practice. The more problems related to math you come across, the easier they will become to understand. You can gain practice directly through coding examples, or you might want to find some algebra and calculus courses online to get better at them


I really wish I understood this…I’m so lost. Why do we need a random number when we have 4 items in the list? Why do we use math random when it only goes to 1 but not 4?How did you know to multiply? I don’t even remember what this is doing anymore.

I can’t do math AT ALL. I never passed algebra in high school that was 20 years ago. I dropped out because I’m an idiot, I have no idea what calculus is either. I wish I knew this math knowledge was necessary before i tried so hard and learned nothing with these lessons. Now i understand why all of this makes no sense because i’m stupid

Yeah, programming can be frustrating sometimes.

We don’t need to have a random number to pick an item from the list. The only reason we are doing it is because that’s what the example (the one in the quiz you shared) asked us to do. It’s not that we need to pick at random every time. We are just solving the question at hand.

Depending on the kind of programming you want to do, it will involve more math or less. You are in the Web Developer Path right? Most of the math you’ll need to use will be measurements like how far to the right or from the bottom you want things to be. Just be patient and learn at your speed.

By the way, did you complete the Code Foundations path before trying Web Development? If you’ve tried programming before it might help! Also the Computer Science path is a great introduction to a lot of concepts you’ll see across disciplines.

If you work through things and feel stuck, asking others around the Forums is a great way to get new perspectives on problems. It helps me a lot!

I wish it was frustrating…I can’t stand learning JavaScript…it’s so complicated and difficult just to say a few words or something so simple. But I have to for work. I’ve asked questions on forums…everyone is so far ahead and it makes no sense. Now I know why… I didn’t know about code foundations or computer science (failed that too)… I just needed to learn JavaScript since I know HTML CSS and PHP but i can’t without math? I really wish i knew that before trying so hard here.

Hello @byte5723385929. You don’t need to know that much maths to be able to understand what’s going on in this case. As @irlfede said, taking those paths is advisable. To reiterate on the question about Math.random(), and what @irlfede said :
This returns a number. This number can be anything from 0 to 1. Say we get the number 0.5. If we just leave it like this, we can’t access anything from the list, so we need to make it bigger. Since there are four items in the list, this make sense, because the largest number we can get is 3.99(reoccurring to to floating point limits-but that isn’t very important). Since there are 4 items in the list, the indexes are as follows:


(Remember, list indexes start at zero.) So, we now have a number, between 0 and 1, that we’ve multiplied by the length of the list, so we can access any element from the list. However, there is no such element as 3.2. That is where Math.floor() comes in. Basically, what this does is it takes the numbers to the left of the decimal point, and chops off the rest:

= 12

because the number before (to the left of) the decimal point is 12, and the function ignores the rest.

Here is some reading on the Math.random() function, here is some reading on the Math.floor() function, and here is some reading on the Math.ceil() function, which is essentially the opposite of the Math.floor() function.

I hope this helps!

Thank you for the reply, i really wish i understood this. Why is it 3.2 and not 4 like the other example?

Is there ANYWHERE on codeacademy I can review this material? I don’t understand why all this stuff is in the quizzes and never in the material in the lessons i’ve reviewed dozensof times and still never learn anything.

I’ll try the links you sent but I think i really need more math than I know…I’ll, still not sure what an integer is. I don’t think I can do this.

This is because (I think, if I understand you correctly), the function generates a random number, so it it could be anything between 0 and 1 (then multiplied as many times as you want.) I think somewhere around the raceday project (maybe even in it), it talks about these functions.

An integer is just a number without any decimal points, and it can be positive or negative:

3 //is an integer
-3//is an integer
56//is an integer
9.8//is not an integer

I hope this helps!

1 Like

I’ve only done two projects and failed them both totally, the weather project and the dog years project. They are almost nothing like what the lessons taught and it’s totally defeating and frustrating to not be able to use anything in the lessons after spending 60+ hours reviewing the same material over and over and over over. Maybe it’s just me?

Integer makes more sense now. I don’t understand how I would be able to think to come up with this solution though…if there are 4 items, can’t we just say 4 instead of doing so many other steps?

I gave explaining Math.floor(Math.random() * n) a shot some time ago. Here’s a link. Possibly it along with the explanations above will help.

*Note: n represents the number of choices like if I asked you to choose a number between 0 and 4. There are 5 possibilities (0, 1, 2, 3, 4), so n would be replaced with 5 -> Math.floor(Math.random() * 5).

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.