/    Sign up×
Community /Pin to ProfileBookmark

Reverse a value

Not sure how to ask this…

I have a value being provided within a range of 0 to 100.
The problem is it is just opposite of what I need to display on the screen.
In that if the value presented is 85 it needs to be 15.

Could someone be so kind as to enlighten me on where to find documentation to reference for this?

Thank you in advance.
Steven

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@cgishackDec 17.2006 — If you just want to find the difference of some number you can try something like so.
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;

function doMath() {
var theValue = document.MyForm.theUsersValue.value;
var theResut = 100 - theValue;
alert(theResut);
}



&lt;/script&gt;


&lt;/head&gt;
&lt;body&gt;

&lt;form name="MyForm" method="post" action=""&gt;
&lt;input type="text" name="theUsersValue"&gt;
&lt;input type="button" name="Submit" value="Submit" onClick="doMath();"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Copy linkTweet thisAlerts:
@you_yes_youDec 17.2006 — ... if I understand your problem... Being x the number provided in the range [0, 100], the number you want to be displayed in the screen is 100-x. This observation helps?
Copy linkTweet thisAlerts:
@konithomimoDec 17.2006 — If you just want to find the difference of some number you can try something like so.
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;

function doMath() {
var theValue = document.MyForm.theUsersValue.value;
var theResut = 100 - theValue;
alert(theResut);
}



&lt;/script&gt;


&lt;/head&gt;
&lt;body&gt;

&lt;form name="MyForm" method="post" action=""&gt;
&lt;input type="text" name="theUsersValue"&gt;
&lt;input type="button" name="Submit" value="Submit" onClick="doMath();"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

[/QUOTE]

You need to parse the input value, so this:

var theValue = document.MyForm.theUsersValue.value;


should be:

var theValue = parseInt(document.MyForm.theUsersValue.value);
Copy linkTweet thisAlerts:
@cgishackDec 17.2006 — You need to parse the input value, so this:

var theValue = document.MyForm.theUsersValue.value;


should be:

var theValue = parseInt(document.MyForm.theUsersValue.value);[/QUOTE]


True Dat!
Copy linkTweet thisAlerts:
@k7faqauthorDec 17.2006 — I should have elaborated more....

This value is being supplied by an OPC tag. (i.e. CHWV.value)

Should the value be provided as 0 then it means the valve has been commanded 100% open. If the value is 100 then the valve was commanded closed.

I need to interpret this and display on the screen a tag that results in the commanded percentage vs the value provided.

I thought of the idea of the x-100. But then what would I do with a negative number? Possibly strip the minus sign if negative before displaying on screen?

Sorry. I rarely come across simplistic problems.

Steven
Copy linkTweet thisAlerts:
@cgishackDec 17.2006 — Maybe something like so now ?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;

function doMath() {
var theValue = Math.abs(document.MyForm.theUsersValue.value);
var theResult = 100 - theValue;

<i> </i>if(!isNaN(theValue) &amp;&amp; theValue &lt;= 100 &amp;&amp; theValue &gt;= 0) {
<i> </i> document.getElementById("theResultTag").innerHTML = theResult + "%";
<i> </i>} else { alert("Invalid Number, It must be above 0 and below 100"); }

}



&lt;/script&gt;


&lt;/head&gt;
&lt;body&gt;

&lt;form name="MyForm" method="post" action=""&gt;
&lt;p&gt;
&lt;input type="text" name="theUsersValue"&gt;
&lt;input type="button" name="Submit" value="Submit" onClick="doMath();"&gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Results:&lt;/p&gt;
&lt;p&gt;&lt;span id="theResultTag"&gt; &lt;/span&gt; &lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@k7faqauthorDec 17.2006 — I really appreciate all the input and assistance!!!!

Still learning js and trying to learn 5 software apps simultaneously to pull of a major job for my employer.

You guys just saved me at least a full day of reading and trying!!!

THANK YOU!

Steven
Copy linkTweet thisAlerts:
@cgishackDec 17.2006 — You can take it a bit further if you want.

The above code is just to answer your exact question.

I can see you would probably want it to say "CLOSED" if the value was equal to 0%

You can do this by doing a small "if statement" at the end of the doMath() function

<i>
</i>if(theResult ==0) {
document.getElementById("theResultTag").innerHTML = "CLOSED"
}

×

Success!

Help @k7faq 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...