JavaScript Program Help School Project

Hello, I am a junior in Highschool and I am taking an AP Computer Science class. I need to make a program to pass the AP test. I am making a mobile app in the AppLab environment from Code.org as instructed by my teacher. My app is about calculating the weight one should put on the bar when working out and also tell the user the best combination of weight plates to use. I was able to make the app tell the user how much weight to put on each side because it is very simple but I am struggling with the plate combinations. I don’t have too much experience with programming so the help would be appreciated. Calculator example I found online: Search up “how much weight to put in the barbell calculator”. Link to my project: (https://studio.code.org/projects/applab/b4u4gKOIekfMra404Jb8JcTaBbz6s4jYUz7TbGYzDYc) . Click “View Code” to see code. I dont’t know what would be the best way to make this calculation.

Two important questions:

  1. What have you tried so far?

  2. How would you calculate the plate combinations by hand? Can that be translated to the computer?

If you look at my code which I understand is confusing and hard to read. I tried “if ((weightOutput % 45 === 0) === true)” to check if something is divisible by a weight that represents a kind of weight plate. I have to to do this for 45 lbs plates, 35 plates, 25 plates, 10 plates, 5 plates, and 2.5 plates. What I have tried is checking if the weight the user had inputted is divisible by 45 then if it is it divides by 45 to get how many 45 plates the user should put on the bar. Then, it subtracts the amount of 45 lbs plates and then it checks if it is divisible by 35 and it should keep going until 2.5 lbs. This example might be helpful Calculator Example.

But is that the way you do it in the gym? If I want a barbell with 150 lbs, then

  1. I want 75 lbs on each side.
  2. I go looking for 45s. I can add 1 on each side. I now need 20 lbs on each side.
  3. I go looking for 35s. Wait! That’s too big. Skip.
  4. I go looking for 25s. Wait! That’s too big. Skip.
  5. I go looking for 10s. I can add 2 on each side. I now need 0 lbs on each side. Done!

Is this what your code is currently doing?

Sometimes when coding we start doing math equations but forget to stop and think about what we are actually doing in the real world.

You are right that what I’m doing is not how a person would do it in real life as only in special cases something would be perfectly divisible. I will have to rethink my approach but I am limited due to my small knowledge. P.S If you wanted to put 150 lbs on the barbell you would put 52.5 lbs on each side because the barbell weighs 45 lbs(on average) so you have to subtract then divide by 2. Thanks for your help.

Right, I knew I was forgetting something!

I came up with a formula I would use but don’t really know how to translate it to the computer well. E.g I want to put 125 lbs in the bar so I do (125-45)/2 which equals 50 lbs to know how much weight to put on each side. Then a computer someone could to something like this 50-45= 5 so more weight is needed. Then, 5-35= -30 so that is bad and keep going until 5-5=0 which means no more weight is needed. I came up with this:

 var fourFivePlates = 0;
 while (weightOutput - 45 >= 0) {
 weightOutput - 45;
 fourFivePlates + 1;
 }
  
  setText("45platesout",fourFivePlates);

Now that you have the right core logic, the problem becomes a question of abstraction. You have what, six plate sizes, right? plateSizes[] = {45, 35, 25, 10, 5, 2.5}?

Can arrays help you reuse your logic above?

I guess that could work but the problem is is that I don’t know how I would use it because I don’t have much experience programming.

Well, I like to use pesudocode when I’m trying to figure things out.

Would this be a reasonable description of the process above:

for (each plate size, from smallest to largest) {
  subtract the maximum valid number of plates
}

Ok, that helps me but I don’t know the syntax for it.

You should work through the Free Code Camp Basic JavaScript section to familiarize yourself with the various syntax and methods available.

1 Like

Thank you @RandellDawson! I was about to give that exact same advice but didn’t have the link.