-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_temperature_conversion.js
More file actions
125 lines (107 loc) · 3 KB
/
10_temperature_conversion.js
File metadata and controls
125 lines (107 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Write a function that converts temperature from one unit to another
Function takes three arguments: `from`, `to`, `value`
`from` and `to` can have following values:
- C
- F
- K
Here C means Celsius, K is Kelvin and F is Fahrenheit
Examples:
convert('C', 'K', 0) => 273.15
convert('C', 'F', 37) => 98.6
convert('F', 'K', 98.6) => 310.15
convert('F', 'C', -40) => -40
convert('K', 'C', 100) => -173.15
convert('K', 'F', 100) => -279.67
Here are the conversion formulae in case you wonder how it is done :)
- F to C:
(F − 32) × 5/9 = C
- K to C:
K − 273.15 = C
**Your function must return a value**
It's not necessary to print the result on screen,
however to test your function you are free to print the result
*/
function convertCelciusToAnotherUnit(convertTo, value) {
let temperature = 0;
switch(convertTo) {
case 'K':
temperature = value + 273.15;
return temperature;
case 'F':
temperature = 9 * value / 5 + 32;
return temperature;
case 'C':
return value;
default:
return NaN;
}
}
function convertFahrenheitToAnotherUnit(convertTo, value) {
let temperature = 0;
switch(convertTo) {
case 'K':
temperature = (value - 32) * 5/9 + 273.15;
return temperature;
case 'C':
temperature = (value - 32) * 5/9;
return temperature;
case 'F':
return value;
default:
return NaN;
}
}
function convertKelvinToAnotherUnit(convertTo, value) {
let temperature = 0;
switch(convertTo) {
case 'F':
temperature = (value - 273.15) * 9 / 5 + 32;
return temperature;
case 'C':
temperature = value - 273.15;
return temperature;
case 'K':
return value;
default:
return NaN;
}
}
function convert(from, convertTo, value) {
let temperature = 0;
switch (from) {
case 'C':
return convertCelciusToAnotherUnit(convertTo, value);
case 'F':
return convertFahrenheitToAnotherUnit(convertTo, value);
case 'K':
return convertKelvinToAnotherUnit(convertTo, value);
}
}
function getMark(isPassed) {
return isPassed ? '✅' : '❌';
}
function getMessage(from, convertTo, expected, actual) {
let message = "Temperature '" + from + "' to '" + convertTo + "' expected: ";
message += expected + " and is ---> " + actual;
return message;
}
function testConvert(from, convertTo, value, expected) {
const actual = convert(from, convertTo, +value);
const isPassed = actual === expected;
console.log(getMark(isPassed), getMessage(from, convertTo, expected, actual));
}
function tests() {
testConvert('C', 'K', 0, 273.15);
testConvert('C', 'K', "0", 273.15);
testConvert('C', 'C', 0, 0);
testConvert('C', 'F', 37, 98.6);
testConvert('F', 'K', 98.6, 310.15);
testConvert('F', 'K', 98.6, 310.15);
testConvert('F', 'F', -40, -40);
testConvert('K', 'K', 100, 100);
testConvert('K', 'k', 100, NaN);
testConvert('K', 'C', 100, -173.15);
testConvert('K', 'F', 100, -279.67);
}
tests();