-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature.js
More file actions
84 lines (75 loc) · 3.43 KB
/
temperature.js
File metadata and controls
84 lines (75 loc) · 3.43 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
// This program allows the user to convert any temperature from either C - Celsius, F - Fahrenheit or K - Kelvin to C, F or K
// It first prompts user for the metric they converting from
let choosemetric = prompt(`In which metric is the temperature you are converting? Type C or F or K:
C - (Celsius)
F - (Fahrenheit)
K - (Kelvin)`);
// This converts the string value entered to an uppercase letter
let metricUppercase = choosemetric.toUpperCase();
// User then inputs the temperature they want to convert
let temperature = prompt("Enter the temperature that you want to convert: ");
// This converts the string value into an integer
let numTemperature = Number(temperature);
// Next it prompts user for the metric they converting to
let nextMetric = prompt(`To which metric would you like to convert the temperature to?"
C - (Celsius)
F - (Fahrenheit)
K - (Kelvin)`);
// This converts the string value entered to an uppercase letter
let convertTo = nextMetric.toLocaleUpperCase();
switch (metricUppercase) {
case "C":
switch (convertTo) {
case "F":
// Input temperature is converted from Celsius to Fahrenheit
let fahrenheit = (numTemperature * 9 / 5 + 32);
let fahrenheitDecimal = fahrenheit.toFixed(2);
console.log((numTemperature + "°C") + " is " + (fahrenheitDecimal + "°F"));
break;
case "K":
// Input temperature is converted from Celsius to Kelvin
let kelvin = ((numTemperature) + 273.15);
let kelvinDecimal = kelvin.toFixed(2);
console.log((numTemperature + "°C") + " is " + (kelvinDecimal + "K"));
break;
console.log("Invalid metic to convert to");
}
break;
case "F":
switch (convertTo) {
case "C":
// Input temperature is converted from Fahrenheit to Celsius
let celsius = ((numTemperature - 32) * 5 / 9);
let celsiusDecimal = celsius.toFixed(2);
console.log((numTemperature + "°F") + " is " + (celsiusDecimal + "°C"));
break;
case "K":
// Input temperature is converted from Fahrenheit to Kelvin
let kelvin = ((numTemperature + 459.67) * 5 / 9);
let kelvinDecimal = kelvin.toFixed(2);
console.log((numTemperature + "°F") + " is " + (kelvinDecimal + "K"));
break;
default:
console.log("Invalid metic to convert to");
}
break;
case "K":
switch (convertTo) {
case "C":
// Input temperature is converted from Kelvin to Celsius
let celsius = ((numTemperature) - 273.15);
let celsiusDecimal = celsius.toFixed(2);
console.log((numTemperature + "K") + " is " + (celsiusDecimal + "°C"));
break;
case "F":
// Input temperature is converted from Kelvin to Fahrenheit
let fahrenheit = ((numTemperature) * 9 / 5 - 459.67);
let fahrenheitDecimal = fahrenheit.toFixed(2);
console.log((numTemperature + "K") + " is " + (fahrenheitDecimal + "°F"));
break;
default:
console.log("Invalid metic to convert to");
}
break;
console.log("Invalid metic to convert to");
}