It's an interesting way. I don't like the ternary operator thing personally, but some good ideas.
What I personally try to do, as a starting point
Both the flipping and the unwrapping of 'else' can be done by the IDE. This leaves:
const theFactorText = (number) => {
if (number % 2 === 0) {
return "Not the factor"
}
if (number % 5 === 0) {
return "That's the factor"
}
if (number < 10) {
return "Not the factor"
}
throw new Error(
"Number doesn't fall under any category. Input another"
)
}
If you work a lot with WordPress, it is always better to put the results into variables and then output them at the end, so you can use a filter.
That's why I would do it the same way Mark does. So always a single if, also because it is easier to read for others.
For all who work with PHP and WordPress, something like this could look like this:
/** * Here we define the description for the job names and output them. * * @param string $job_name * * @return string */ function get_job_description( $job_name ) { $job_description = "I'm not sure, I'd google it"; if ( 'painter' === $job_name ) { $job_description = 'Paints on canvas for art lovers'; } if ( 'developer' === $job_name ) { $job_description = 'Writes code which runs on machines'; } return apply_filters( 'job_description', $job_description, $job_name ); } /** * Now other users can use WordPress for their projects, change or extend the description. * * @param string $job_description * @param string $job_name * * @return string */ add_filter( 'job_description', function( $job_description, $job_name ) { if ( 'gamer' === $job_name ) { $job_description = 'Is a gamer and plays on pc master race'; } return $job_description; }, 10, 2 );Well, I always try to build in a way that others with hooks can rework the function and read it at any time. Hence the individual IF blocks.