javascript - How can I scale an array of values while maintaining a minimum value? -


so have array of values need scale down while maintaining minimum value scaled value.

for example, let's have array of values [1, 1, 3, 5] minimum scale factor of .2.

by normalizing array, array [.1, .1, .3, .5]. however, keeping in mind minimum scale factor, we'd have values [.2, .2, .3, .5], adds 1.2, not 1.

my thinking iterate on array , first set values under minimum minimum, keeping carry variable determine how still needs redistributed other elements in array on minimum.

with of values on minimum, scale values respect carry variable , subtract values.

so example above, we'd subtract 3/8 * .2 .3, , 5/8 * .2 .5 [.2, .2, .225, .375].

is there other way more efficiently? or alternative way scale remaining values?


edit: sorry, scaling might incorrect term, in end values of array divided in such way values changed respect total value.

i'll explain specific implementation question might more clear:

i have number of posts, , each of posts shown amount of time before fading out, after next post shown. want delay between posts dependent on number of words within each post, constrained @ least minimum value.

there total amount of time of posts shown, , time supposed split between of posts.

i want delay between posts dependent on number of words within each post, constrained @ least minimum value.

there total amount of time of posts shown, , time supposed split between of posts.

you cannot guarantee you'll meet both requirements. if have 30 posts, each of must displayed @ least 1 second, , 20 seconds in display them, it's impossible meet both requirements. you'll have to:

  1. extend total time; or
  2. reduce minimum time

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -