Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tip #03 - Improve Nested Conditionals example should have vastly different functions to call #66

Closed
petetnt opened this issue Jan 11, 2016 · 5 comments

Comments

@petetnt
Copy link
Contributor

petetnt commented Jan 11, 2016

The question of the tip is formatted as follows:

How can we improve and make more efficient nested if statement in javascript.

if (color) {
  if (color === 'black') {
    printBlackBackground();
  } else if (color === 'red') {
    printRedBackground();
  } else if (color === 'blue') {
    printBlueBackground();
  } else if (color === 'green') {
    printGreenBackground();
  } else {
    printYellowBackground();
  }
}

With the given information (the if-clause and the function names), the most obvious choice would be just to refactor the functions itself

  printBackground(color);

  /**/

  function printBackground(color = "yellow") {
     // do something with the color, default being yellow
  }

which is a good tip in itself, but maybe not in the same vain the original #3 is. Obviously this isn't always possible (or feasible) with all of the cases, so the example could be / should be easily modified to something that looks more non-trivial 😸

@loverajoel
Copy link
Owner

@AlbertoFuente ping!

@shivarajnaidu
Copy link
Contributor

Can we make use of functions ,for loop and if combination.. For if we provided with too much number of choices ?
How about this dears ? 😄

@shivarajnaidu
Copy link
Contributor

Or can we use switch case ??

@zenopopovici
Copy link
Collaborator

A function would be the most obvious choice here. Switch can work too, but in this case it should be a function.
@petetnt Can you add a PR that completes the example with a function as you suggested?

@zenopopovici zenopopovici changed the title #03 - Improve Nested Conditionals example should have vastly different functions to call Tip #03 - Improve Nested Conditionals example should have vastly different functions to call Feb 20, 2016
@petetnt
Copy link
Contributor Author

petetnt commented Mar 5, 2016

Whoops @zenopopovici missed that @-mention and did see it until this was closed. I can scratch up a PR though now quickly 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants