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

Roman Smolkin
Roman Smolkin
469 Points

Simplify increment/decrement into changeScore function? But what's the syntax for passing a 1 or an -1 to changeScore?

Since increment and decrement score functions are so similar, it's probably better to use use one function for ChangeScore that take a parameter of 1 or -1. But I'm not sure exactly what that syntax should be. Also, if the initial state was coming from a value in DB and needed to be synced between this app on several devices where anyone pressing the button to increment or decrement would update all the other ones, that's an example I'd love to see.

Charles Wanjohi
Charles Wanjohi
9,235 Points

A function that takes the totalScore and either 1 or -1 can achieve this:

function changeScore(totalScore,scoreChange){

return totalScore+scoreChange;
}

//call the function with total scrore and 1 or -1

var totalScore=0;
totalScore=changeScore(totalScore,-1); //to decrement

totalScore=changeScore(totalScore,1); //to increment

Hope this help you.If not let me know