tiscavalcanti Posted December 18, 2015 Share Posted December 18, 2015 Hello, I have created this code: if (w > maxWidth || h > textHeight) { if (y < x) { [y, y] } else { [x, x] } } else { [value[0], value[1]] } It is a piece, but it gives to understand what I want to optimize without the rest. How to modify this code to work with: Switch Statement instead If/Else mode? This is a expression of After Effects. Thanks. Link to comment Share on other sites More sharing options...
john_jack Posted December 18, 2015 Share Posted December 18, 2015 take a look at this http://www.w3schools.com/js/js_switch.asp Link to comment Share on other sites More sharing options...
Ingolme Posted December 18, 2015 Share Posted December 18, 2015 The switch statement is not the optimal structure to use in this situation. Why do you want to use it? Link to comment Share on other sites More sharing options...
tiscavalcanti Posted December 18, 2015 Author Share Posted December 18, 2015 The switch statement is not the optimal structure to use in this situation. Why do you want to use it? And something like "switch(true)" work? If not, could edit this code for me, in If/Else same, of a better way? Thanks! Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2015 Share Posted December 18, 2015 It would, but if you're doing a switch on a boolean it usually makes more sense to use an if/else structure. If your goal is optimization, you don't see big performance increases by using a switch over an if/else structure. Link to comment Share on other sites More sharing options...
tiscavalcanti Posted December 18, 2015 Author Share Posted December 18, 2015 (edited) It would, but if you're doing a switch on a boolean it usually makes more sense to use an if/else structure. If your goal is optimization, you don't see big performance increases by using a switch over an if/else structure. I can do this: function getValue() { switch (true) { case yB < xA: return [yB, yB]; default: return [xA, xA]; } } E = getValue(); if (w > maxWidth || h > textHeight){ x = E[0]; y = E[1]; } [x, y]; Edited December 18, 2015 by tiscavalcanti Link to comment Share on other sites More sharing options...
davej Posted December 18, 2015 Share Posted December 18, 2015 I can do this: Uh, no you can't. Link to comment Share on other sites More sharing options...
tiscavalcanti Posted December 18, 2015 Author Share Posted December 18, 2015 Uh, no you can't. Why not? Link to comment Share on other sites More sharing options...
davej Posted December 18, 2015 Share Posted December 18, 2015 Hmmm... it does work, but what is the point? It reminds me of people who use for( ; ; ){} rather than while(true){} Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2015 Share Posted December 18, 2015 I can do this:Yes, you can, but since we're talking about optimizing and efficiency, what does this do for you: switch (true) { case yB < xA: return [yB, yB]; default: return [xA, xA]; }that is better than this: if (yB < xA) { return [yB, yB]; } else { return [xA, xA]; }or this: return (yB < xA) ? [yB, yB] : [xA, xA]; Link to comment Share on other sites More sharing options...
tiscavalcanti Posted December 18, 2015 Author Share Posted December 18, 2015 Really, thanks for all, justsomeguy davej Ingolme In fact, I wanted to know different ways of dealing with conditions a little more complex. You all helped me realize how many ways I can do the same thing, that's what I needed really. I expressed myself badly. Thanks again! Link to comment Share on other sites More sharing options...
tiscavalcanti Posted December 19, 2015 Author Share Posted December 19, 2015 Yes, you can, but since we're talking about optimizing and efficiency, what does this do for you: switch (true) { case yB < xA: return [yB, yB]; default: return [xA, xA]; }that is better than this: if (yB < xA) { return [yB, yB]; } else { return [xA, xA]; }or this: return (yB < xA) ? [yB, yB] : [xA, xA]; This way is interesting: return (yB <xA)? [yB, yB]: [xA, xA]; Could you explain a little more how it works. The use of interrogation, is something new to me. Link to comment Share on other sites More sharing options...
dsonesuk Posted December 19, 2015 Share Posted December 19, 2015 Explained here http://www.w3schools.com/js/js_comparisons.asp under 'Conditional (Ternary) Operator'. Link to comment Share on other sites More sharing options...
tiscavalcanti Posted December 19, 2015 Author Share Posted December 19, 2015 Explained here http://www.w3schools.com/js/js_comparisons.asp under 'Conditional (Ternary) Operator'. Thank you too! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now