3 ms·
This was a lot of work and you should be really proud. One suggestion which could simplify things. I noticed this bit of code: var getNumberColor = function
by schwartzworld 7y ago
This was a lot of work and you should be really proud.
One suggestion which could simplify things. I noticed this bit of code:
var getNumberColor = function( number )
{
var color = 'black';
if (number === 1)
{
color = 'blue';
}
if (number === 2)
{
color = 'green';
}
// etc
}
You mention using a switch statement, but why not just use an array and look it up by the index?
- rezistik 7y agoAnd why not just return the result instead of assigning it to something you eventually return?
- winrid 7y agoThe argument against this is usually one return statement is preferred.
- maynman 7y agoYep, that's why I chose to implement it that way. I generally prefer single return statements. Plus my day job enforces it as part of the coding convention, so force of habit too haha.
- gatherhunterer 7y agoThe initial assignment is used as a base case. Returns in a conditional statement are often used as an "escape" when you want to get out of a function (e.g. null checks). This is purely a stylistic choice whereas using constant time lookup over conditional evaluation could be argued to have some performance impact (although in this case it would not be meaningful). That said, I don't see anything about this choice that needs to be defended.