output only when greater than zero

I'm new to javascript, could someone tell me how to only print the output of the following expression if this.y > 0, thanks!
"(function(){return this.y + '('+this.percentage.toFixed(0)+'%'+')';})"
Andrew DownsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
how to only print the output of the following expression if this.y > 0

I am not quite an expert in this area, however, you cannot choose either return a value or not return a value in a function. (Hope you get what I am trying to say here.....!)

So, technically, if this.y is 0 your function will still either return null or an empty string, depending on how we code it.

Now, assuming the caller simply displays whatever is returned, it fails. So basically suppress any display / output / log / etc to act if this.y == 0 then it should be handled before this function is called.

Have I made it complicated enough for you understand?
0
Andrew DownsAuthor Commented:
I have no idea honestly.  I just need to know the new expression to not output any values of zero
0
Nitin SontakkeDeveloperCommented:
Would empty string do? If yes, may be as follows, try it. As I said I am not quite an expert.

(
  function()
    {
      if (parseInt(this.y) == 0) return '';
      return this.y + '('+this.percentage.toFixed(0)+'%)';
    }
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Olaf DoschkeSoftware DeveloperCommented:
This doesn't output anything, it returns a value to a caller. If you meant to say how to return the empty string if y=0, then that's a direct if statement in the form (condition) ? ifvalue : elsevalue;

Anyway, why not make it more verbose, you're allowed to put more code before the return inside a function body, you can even let it span multiple lines, and add a comment:

function(){
var returnvalue= '';
// only show values >0:
if (this.y>0) ret = '('+this.percentage.toFixed(0)+'%'+')';
return returnvalue;
}

Open in new window


Bye, Olaf.
0
Nitin SontakkeDeveloperCommented:
You mention > 0 but then what is expected data type of y? Is it float? What is expected min value of this.y?
0
Olaf DoschkeSoftware DeveloperCommented:
correction:
function(){
var returnvalue= '';
// only show values >0:
if (this.y>0) returnvalue = '('+this.percentage.toFixed(0)+'%'+')';
return returnvalue;
}

Open in new window

0
Nitin SontakkeDeveloperCommented:
@Olaf, am I right in stating that there is typo in your code? ret never used.

if (this.y > 0) returnvalue = '('+this.percentage.toFixed(0)+'%'+')';

Open in new window

0
Olaf DoschkeSoftware DeveloperCommented:
Yes, I posted the correction.

...and I still forgot to put this.y in front.
0
Andrew DownsAuthor Commented:
Thank you so much!!!!
0
Olaf DoschkeSoftware DeveloperCommented:
The overall solution should be
function(){
var returnvalue= '';
// only show values >0:
if (this.y>0) returnvalue = this.y+'('+this.percentage.toFixed(0)+'%'+')';
return returnvalue;
}

Open in new window

or what Nitin posted in https://www.experts-exchange.com/questions/29091604/output-only-when-greater-than-zero.html#a42514708
0
Olaf DoschkeSoftware DeveloperCommented:
Both solutions work, Nitin was first. I contributed with an alternative and explaining the possible use of the ternary operator.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.