admin管理员组

文章数量:1203546

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!

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!

Share Improve this question edited Jul 29, 2010 at 11:29 Tom Gullen asked Jul 29, 2010 at 11:14 Tom GullenTom Gullen 61.7k87 gold badges291 silver badges469 bronze badges 14
  • Show your existing code. – Daniel Egeberg Commented Jul 29, 2010 at 11:15
  • How is it 'stepping down'? You probably want to change your loop. Otherwise you can use (50 - <your number>) – adamnfish Commented Jul 29, 2010 at 11:16
  • I've appeared to have misphrased the question, there is not a loop whose indexes I can manipulate, the value that steps down represents a % of how far through the first half of the animation is complete. – Tom Gullen Commented Jul 29, 2010 at 11:20
  • 1 No problem I've rephrased the question now incase anyone finds it at a later date – Tom Gullen Commented Jul 29, 2010 at 11:29
  • 3 -1 for the answer is a very simple math equation which a 10 year old can answer – Numenor Commented Jul 29, 2010 at 11:52
 |  Show 9 more comments

4 Answers 4

Reset to default 13

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.

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
|_____|_____|_____|           :      :        :

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.

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>");}

本文标签: javascriptReverse a percentageStack Overflow