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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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?
Andrew DownsAuthor Commented:
I have no idea honestly.  I just need to know the new expression to not output any values of zero
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

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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.
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?
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

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

Olaf DoschkeSoftware DeveloperCommented:
Yes, I posted the correction.

...and I still forgot to put this.y in front.
Andrew DownsAuthor Commented:
Thank you so much!!!!
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
Olaf DoschkeSoftware DeveloperCommented:
Both solutions work, Nitin was first. I contributed with an alternative and explaining the possible use of the ternary operator.
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.