views:

173

answers:

4

I have a percentage, it ranges from 50% to 0%.

I need the values to be mirrored, so:

0% now equals 50%
1% = 49%
25% = 25%
48% = 2%
50% = 0%

etc.

Thanks for any help!

+9  A: 

You can use j = max_i - i + min_i where the two constants min_i and max_i are the lower and upper limit of the range.

If i is always between 0 and 50 then you can just write j = 50 - i.

Mark Byers
A: 
var min=1;
var max=50;
for(var i=min;i<=max;i++){document.writeln(i + "<br>");}
for(var i=max;i>=min;i--){document.writeln(i + "<br>");}
Tim Schmelter
+1  A: 

If i'm reading that correctly, the only way for the pcntAnimationComplt to go down is if your currImgWidth is decreasing. If that is so, then just do this:

pcntAnimationComplt = 50 - Math.round((parseFloat(currImgWidth / pageWidth) * 100) / 2);

This should go from 0 to 50, as per your requirements.

slugster
+11  A: 

It looks like you want to define a function like this:

(x)      f(x)
 0        50
 1        49
 2        48
 :         :
48         2
49         1
50         0

Then the function is simply:

f(x) = 50 - x

More generally, if x is between low and high inclusive, then:

f(x) = (high + low) - x

Other functions of interest

Here are some other common functions:

(x)    f(x)___
 0       0    |
 1       0    3
 2       0 ___|
 3       1    |
 4       1    3     f(x) = x / 3
 5       1 ___|           where / is integer division
 6       2    |
 7       2    3
 :       : ___|

(x)    f(x)___
 0       0    |
 1       1    3
 2       2 ___|
 3       0    |
 4       1    3     f(x) = x % 3
 5       2 ___|           where % is integer remainder
 6       0    |
 7       1    3
 :       : ___|

Both of the above are sometimes combined when indexing a 2-dimensional table:

  ______4 columns______
 /                     \
 _______________________     (x)   row(x)   col(x)
|     |     |     |     |     0      0        0
|  0  |  1  |  2  |  3  |     1      0        1
|_____|_____|_____|_____|     2      0        2      row(x) = x / 4
|     |     |     |     |     3      0        3      col(x) = x % 4
|  4  |  5  |  6  |  7  |     4      1        0
|_____|_____|_____|_____|     5      1        1      x = row(x) * 4 + col(x)
|     |     |     |           6      1        2
|  8  |  9  | ... |           7      1        3
|_____|_____|_____|           :      :        :
polygenelubricants
Upvoting you not because of your ability to answer a question directly, but because of a nice implicit demonstration of a way to solve problems in general.
Noon Silk
+1 for awesome ASCII art.
Brad Hein