Skip to content

Commit 30092c9

Browse files
author
Mark Hatchell
committed
added resistor divider class.
1 parent 1252748 commit 30092c9

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

src/Resistor-Divider.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
/**
4+
*
5+
* ResistorDivider is a class with methods that can be used without initializing.
6+
*
7+
* It provides the various methods for determining one of the four variables for each calculation.
8+
* if (calcVolts)
9+
{
10+
volts = voltsout*(ohms1+ohms2)/ohms2;
11+
}
12+
else if (calcOhms1)
13+
{
14+
ohms1 = volts*ohms2/voltsout-ohms2;
15+
}
16+
else if (calcOhms2)
17+
{
18+
ohms2 = voltsout*ohms1/(volts-voltsout);
19+
}
20+
else if (calcVoltsout)
21+
{
22+
voltsout = volts*ohms2/(ohms1+ohms2);
23+
}
24+
* @example
25+
*
26+
*/
27+
class ResistorDivider {
28+
29+
/**
30+
*
31+
* equation is outputVoltage = (inputVoltage * resistor2) / (resistor1 + resistor2)
32+
*
33+
* @param {number} inputVoltage
34+
* @param {number} resistor1
35+
* @param {number} resistor2
36+
* @returns {{outputVoltage: number}}
37+
*/
38+
static calcOutputVoltage(inputVoltage, resistor1, resistor2) {
39+
const outputVoltage = (inputVoltage * resistor2) / (resistor1 + resistor2);
40+
return {
41+
outputVoltage
42+
};
43+
}
44+
45+
static calcResistor2(inputVoltage, outputVoltage, resistor1) {
46+
const resistor2 = (outputVoltage * resistor1) / (inputVoltage - outputVoltage);
47+
return {
48+
resistor2
49+
};
50+
}
51+
52+
static calcResistor1(inputVoltage, outputVoltage, resistor2) {
53+
const resistor1 = ((inputVoltage * resistor2) / outputVoltage) - resistor2;
54+
return {
55+
resistor1
56+
};
57+
}
58+
59+
static calcInputVoltage(outputVoltage, resistor1, resistor2) {
60+
const inputVoltage = ((outputVoltage * (resistor1 + resistor2)) / resistor2);
61+
return {
62+
inputVoltage
63+
};
64+
}
65+
66+
}
67+
68+
69+
export default ResistorDivider;
70+

src/Resistor-Divider.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const ResistorDivider = require("./Resistor-Divider");
2+
3+
4+
describe('ResistorDivider', () => {
5+
6+
test('if input voltage is 10 and both resistors are 1000 then output voltage should be 5', () => {
7+
expect(ResistorDivider.calcOutputVoltage(10, 1000, 1000)).toEqual({
8+
outputVoltage: 5
9+
})
10+
});
11+
12+
test('if input voltage is 10, resistor1 is 10000, and resistor2 is 780 then output voltage should be 0.7235621521335807', () => {
13+
expect(ResistorDivider.calcOutputVoltage(10, 10000, 780)).toEqual({
14+
outputVoltage: 0.7235621521335807
15+
})
16+
});
17+
18+
test('if input voltage is 10, resistor1 is 1000, and output voltage is 5 then resistor 2 should be 1000', () => {
19+
expect(ResistorDivider.calcResistor2(10, 5, 1000)).toEqual({
20+
resistor2: 1000
21+
})
22+
});
23+
24+
test('if input voltage is 10, resistor1 is 10000, and output voltage is 0.7235621521335807 then resistor 2 should be 780', () => {
25+
expect(ResistorDivider.calcResistor2(10, 0.7235621521335807, 10000)).toEqual({
26+
resistor2: 780
27+
})
28+
});
29+
30+
test('if input voltage is 10, resistor2 is 1000, and output voltage is 5 then resistor 1 should be 1000', () => {
31+
expect(ResistorDivider.calcResistor1(10, 5, 1000)).toEqual({
32+
resistor1: 1000
33+
})
34+
});
35+
36+
test('if input voltage is 10, resistor2 is 780, and output voltage is 0.7235621521335807 then resistor 1 should be 10000', () => {
37+
expect(ResistorDivider.calcResistor1(10, 0.7235621521335807, 780)).toEqual({
38+
resistor1: 10000
39+
})
40+
});
41+
42+
test('if output voltage is 5 and both resistors are 1000 then input voltage should be 10', () => {
43+
expect(ResistorDivider.calcInputVoltage(5, 1000, 1000)).toEqual({
44+
inputVoltage: 10
45+
})
46+
});
47+
48+
test('if output voltage is 0.7235621521335807, resistor1 is 10000, and resistor2 is 780 then then input voltage should be 9.999999999999998', () => {
49+
expect(ResistorDivider.calcInputVoltage(0.7235621521335807, 10000, 780)).toEqual({
50+
inputVoltage: 9.999999999999998
51+
})
52+
});
53+
54+
55+
56+
57+
});

0 commit comments

Comments
 (0)