Skip to content

Commit a339661

Browse files
committed
feat: Added percent
1 parent 66d88e0 commit a339661

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

docs/index.html

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ <h3 class='mb0 no-anchor'></h3>
273273

274274
</li>
275275

276+
277+
<li><a
278+
href='#percent'
279+
class="">
280+
percent
281+
282+
</a>
283+
284+
</li>
285+
276286
</ul>
277287
</div>
278288
<div class='mt1 h6 quiet'>
@@ -2187,6 +2197,97 @@ <h3 class='fl m0' id='iseven'>
21872197

21882198

21892199

2200+
</section>
2201+
2202+
2203+
2204+
2205+
<section class='p2 mb2 clearfix bg-white minishadow'>
2206+
2207+
2208+
<div class='clearfix'>
2209+
<h3 class='fl m0' id='percent'>
2210+
percent
2211+
</h3>
2212+
2213+
</div>
2214+
2215+
2216+
<p>Work out what percentage value <code>a</code> is of value <code>b</code> using the given base.</p>
2217+
2218+
2219+
<div class='pre p1 fill-light mt0'>percent(number: integer, a: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>, b: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>, base: [<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>]): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></div>
2220+
2221+
2222+
2223+
2224+
2225+
2226+
2227+
2228+
2229+
2230+
<div class='py1 quiet mt1 prose-big'>Parameters</div>
2231+
<div class='prose'>
2232+
2233+
<div class='space-bottom0'>
2234+
<div>
2235+
<span class='code bold'>number</span> <code class='quiet'>(integer)</code> The number to check.
2236+
2237+
</div>
2238+
2239+
</div>
2240+
2241+
<div class='space-bottom0'>
2242+
<div>
2243+
<span class='code bold'>a</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>)</code> The value to work out the percentage for.
2244+
2245+
</div>
2246+
2247+
</div>
2248+
2249+
<div class='space-bottom0'>
2250+
<div>
2251+
<span class='code bold'>b</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>)</code> The value you wish to get the percentage of.
2252+
2253+
</div>
2254+
2255+
</div>
2256+
2257+
<div class='space-bottom0'>
2258+
<div>
2259+
<span class='code bold'>base</span> <code class='quiet'>([<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>]
2260+
(default <code>0</code>)
2261+
)</code> The base value.
2262+
2263+
</div>
2264+
2265+
</div>
2266+
2267+
</div>
2268+
2269+
2270+
2271+
2272+
2273+
2274+
<div class='py1 quiet mt1 prose-big'>Returns</div>
2275+
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a></code>:
2276+
The percentage a is of b, between 0 and 1.
2277+
2278+
2279+
2280+
2281+
2282+
2283+
2284+
2285+
2286+
2287+
2288+
2289+
2290+
21902291
</section>
21912292

21922293

src/math-toolbox.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export { minSub } from './min-sub'
2323
export { average } from './average'
2424
export { isOdd } from './is-odd'
2525
export { isEven } from './is-even'
26+
export { percent } from './percent'
27+
// difference

src/percent.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Work out what percentage value `a` is of value `b` using the given base.
3+
*
4+
* @param {integer} number - The number to check.
5+
* @param {number} a - The value to work out the percentage for.
6+
* @param {number} b - The value you wish to get the percentage of.
7+
* @param {number} [base=0] - The base value.
8+
* @return {number} The percentage a is of b, between 0 and 1.
9+
*/
10+
function percent (a, b, base = 0) {
11+
if (a > b || base > b) {
12+
return 1
13+
} else if (a < base || base > a) {
14+
return 0
15+
} else {
16+
return (a - base) / b
17+
}
18+
}
19+
20+
export { percent }

test/percent.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { percent } from '../src/math-toolbox'
2+
3+
describe('Work out what percentage value', () => {
4+
it('Expect 125 of 100 to be 1', () => {
5+
expect(percent(125, 100)).toBe(1)
6+
})
7+
8+
it('Expect 1 of 100 to in base 2 to be 0.5', () => {
9+
expect(percent(1, 100, 2)).toBe(0)
10+
})
11+
12+
it('Expect 50 of 100 to be 0.5', () => {
13+
expect(percent(50, 100)).toBe(0.5)
14+
})
15+
})

0 commit comments

Comments
 (0)