Expand my Community achievements bar.

SOLVED

How to use divide "/" function and between (50 to 100) in an if/else statement

Avatar

Level 3

Hello,

 

In my list, there are two columns, Men and Women. Men value is '10', Women value is '5'.

I wan to convert following formula to use in an if else statement:

 

if (Men - Women) / Men == '50' to '100' then show this...

I am using following but its not working:

 

<% if (Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men) =='50' to'100" )  { show this}

else { show this} %>

 

Any idea how to edit this statement to make it work?

 

Thanks!

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 9

Hi @alik98709228 - you can try something like this:

<% if ( ((Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men)) >= 50 && ((Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men)) <= 100 )  {...} else {...} %>

 Cheers

Darren

View solution in original post

5 Replies

Avatar

Correct answer by
Level 9

Hi @alik98709228 - you can try something like this:

<% if ( ((Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men)) >= 50 && ((Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men)) <= 100 )  {...} else {...} %>

 Cheers

Darren

Avatar

Level 3

Hi Darren,

 

I tried but I am keep getting syntax error:

 

<% if ( ((Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men)) >= 50 && ((Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men)) <= 100 ) {...} else {...} %>

 

Do you think 50 and 100 should be within quotation marks like '50' and '100'?

And there should be "else if" not just "else"?

 

Let me know.

 

Thanks!

 

Avatar

Community Advisor
@alik98709228 If the return of ((Number(targetData.Men) - Number(targetData.Women)) / Number(targetData.Men)) is a string then use the quotation marks. If the return data type is integer then keep it without quotations marks. it should be just If and else. Else if is required when you have multiple conditions

     Manoj
     Find me on LinkedIn

Avatar

Level 9
Since you are converting the targetData attributes to numbers using Number() to perform a calculation (Men-Women)/Men then you need to compare the result of that to a number. If you add the quotes in, it will try to compare a number to a string. You also dont want an if else() statement in the since you only have two options (i.e. between 50 and 100 or everything else). To debug things like this, you can output the calculation part to the email as plain text and see what value you are getting. Things like targetData.Men = 0 will give you a divide by zero error so you might need to sanitise the results. Hope this helps. Darren

Avatar

Level 3
Hi Darren and Manoj, It worked. Thanks for your feedback!!!