开发者

transform into if/else statement

开发者 https://www.devze.com 2023-02-23 04:10 出处:网络
somehow this doesn\'t work: if(direction != \'leftnav\') direction开发者_如何学Go = \'leftnav\';

somehow this doesn't work:

if(direction != 'leftnav') direction开发者_如何学Go = 'leftnav'; 
if(direction == 'leftnav') direction = 'rightie';

this always comes out as "rightie"

I tend to believe this is because they kind of overwrite. Can this be done with an if else statement? thanks :)


!== and === are strict equality checks.

You probably just want !=, like the following:

if (direction != 'leftnav') {
  direction = 'leftnav';
}
else {
  direction = 'rightie';
}

Or better yet for these simple assignments, using a ternary operator:

direction = direction == 'leftnav' ? 'rightie' : 'leftnav';


Of course they overwrite, look at what you're doing there:

if(direction !== 'leftnav') direction = 'leftnav';
if(direction == 'leftnav') direction = 'rightie';

The first statement sets direction to 'leftnav' if it is anything else. So by the time you get to the second statement, direction will always be 'leftnav'. Did you mean to say else if rather than just if for the second?


Think about the logic you are asking for:

If it is not leftnav, then make it leftnav.
[Next statement -- after it has already been reassigned]
If it is leftnav, make it rightie.

You're looking for

if(direction !== 'leftnav') direction = 'leftnav';
else if(direction == 'leftnav') direction = 'rightie'; // can just be "else"
0

精彩评论

暂无评论...
验证码 换一张
取 消