Skip to content

Commit ca5c170

Browse files
authored
Create Sum and Multiply.js
1 parent 0cb6cd2 commit ca5c170

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Sum and Multiply.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Description:
3+
Write a function that accepts two parameters (sum and multiply) and find two numbers [x, y], where x + y = sum and x * y = multiply.
4+
5+
Example:
6+
7+
sum = 12 and multiply = 32
8+
9+
In this case, x equals 4 and y equals 8.
10+
11+
x = 4
12+
13+
y = 8
14+
15+
Because
16+
17+
x + y = 4 + 8 = 12 = sum
18+
19+
x * y = 4 * 8 = 32 = multiply
20+
21+
The result should be [4, 8].
22+
23+
Note:
24+
25+
0 <= x <= 1000
26+
27+
0 <= y <= 1000
28+
29+
If there is no solution, your function should return null (or None in python).
30+
31+
You should return an array (list in python) containing the two values [x, y] and it should be sorted in ascending order.
32+
33+
One last thing: x and y are integers (no decimals).
34+
*/
35+
var sumAndMultiply = function(sum, multiply) {
36+
for (let i=0;i<=sum;i++){
37+
for (let j=0;j<=i;j++){
38+
if (i+j===sum&&i*j===multiply)
39+
return [j,i]
40+
}
41+
}
42+
return null
43+
}

0 commit comments

Comments
 (0)