/    Sign up×
Community /Pin to ProfileBookmark

conditional operator ?:

Hi,
I would like to get into the habit of using the ?: operator more often but alas,
I can’t seem to ever make it work and revert to a more familiar habit of IF
So, what is the wrong with the code here?

[code=php]function refile(x){
(x.charAt(0)==’C’) ? x=’file:///’+x.replace(/\/g, “/”); return x : return x;

// if(x.charAt(0)==’C’){ x=’file:///’+x.replace(/\/g, “/”); return x}
// else{return x}

}[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERDec 20.2007 — First, while I'm not positive about this, I believe you cannot have 2 statements in one assignment.

Second, in your commented out section, there is no need for the extra 'return x'

Could just as easily been coded:

if (...) { x=...}

return x;
Copy linkTweet thisAlerts:
@samliewDec 20.2007 — try

(x.charAt(0)=='C')</URL> ? x='file:///'+x.replace(//g, "/") : void(0);<br/>
return x;


also, in your second piece of code, you could just do this

if(x.charAt(0)=='C') x='file:///'+x.replace(//g, "/");

return x;
Copy linkTweet thisAlerts:
@Declan1991Dec 21.2007 — or
[code=php]
x = (condition)?value:null;
[/code]

That is the normal way to do it.
×

Success!

Help @justinbarneskin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...