Basketball Scoreboard | Complete beginner to JavaScript looking for feedback

Hey people, I’m not sure if this subforum is exclusively for feedbacks of projects from the freeCodeCamp curriculum, if yes please call me out lol

This was my first JS project ever, just a simple basketball scoreboard and the main thing I’m asking advice/feedback for is my javascript code. My gut feeling tells me I wrote extra code unnecessarily when I could’ve gotten the job done with way less lines by declaring more variables.

2 Likes

Sometimes that’s the case.

But consider this:

sometimes single line of code can be very ‘heavy’ and inefficient. And it’s better to end up with more actual lines.

Not sure about your case tho, I wasn’t messing around JS+HTML situation yet.

1 Like

For the Javascript, you have 6 functions all doing something similar; incrementing the count by x amount for y score card.

An example of simplifying this would be to pass a parameter, count to the function which would be the amount to increment. This will reduce the functions written because you are currently just repeating your code with a slight variation.

In your onclicks you pass the parameter and there you go.

You can do something similar for the home vs guest but start with the count first.

1 Like

thank you! That’s exactly the type of advice I was looking for, I knew I was being repetitive with the functions, I just had no idea how to solve this problem, so thanks!

Nice html and css on your site! I agree that I think you could reduce the length of your code as there are repeating lines of code.

For example, your functions: homeAddPoint, addTwoPoints, addThreePoints fulfill the same purpose of appending to a total score. You could instead make one function that’s more general called homeAddPoints(score) where score is 1, 2 or 3.

You could break it down even further into a function that takes both the points and a variable that determines which scoreboard to update. Like addPoints(team, score).

Once you notice these repeating blocks or lines of code, you can start to make them more general!

Smaller points of notes: you could write homeCount = homeCount+1 as homeCount += 1 but that’s just a small little thing - it doesn’t impact the functionality at all.

1 Like

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