forked from wdi-sg/temperature_converter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (64 loc) · 1.72 KB
/
script.js
File metadata and controls
79 lines (64 loc) · 1.72 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
var STARTING_TEMP, tempValue, temperatureUnit, tempUnit, CONVERTED_TEMP_1, CONVERTED_TEMP_2, arrayUnits, cont, contValue;
//ask for temperature value
function askValue() {
STARTING_TEMP = prompt("Please enter a temperature value (numerical value only):");
tempValue = parseFloat(STARTING_TEMP);
}
//ask for temperature unit
function askUnit() {
temperatureUnit = prompt("Please enter a temperature unit ('C' for degrees Celsius, 'F' for degrees Fahrenheit, or 'K' for degrees Kelvin):");
tempUnit = temperatureUnit.toUpperCase();
}
//convert C to F and K
function convertC(temp) {
CONVERTED_TEMP_1 = temp * 1.8 + 32;
CONVERTED_TEMP_2 = temp + 273.15;
arrayUnits = ["\xB0C", "\xB0F", "K"];
}
//convert F to C and K
function convertF(temp) {
CONVERTED_TEMP_1 = (temp - 32) / 1.8;
CONVERTED_TEMP_2 = CONVERTED_TEMP_1 + 273.15;
arrayUnits = ["\xB0F", "\xB0C", "K"];
}
//convert K to C and F
function convertK(temp) {
CONVERTED_TEMP_1 = temp - 273.15;
CONVERTED_TEMP_2 = CONVERTED_TEMP_1 * 1.8 + 32;
arrayUnits = ["K", "\xB0C", "\xB0F"];
}
while (true) {
var result = "";
askValue();
while (isNaN(tempValue)) {
askValue();
}
askUnit();
while (tempUnit !== 'C' && tempUnit !== 'F' && tempUnit !== 'K') {
askUnit();
}
switch(tempUnit) {
case "C":
convertC(tempValue);
break;
case "F":
convertF(tempValue);
break;
case "K":
convertK(tempValue);
break;
}
temps = [STARTING_TEMP, CONVERTED_TEMP_1.toFixed(2), CONVERTED_TEMP_2.toFixed(2)];
for (var i = 0; i < 3; i++) {
result += temps[i] + arrayUnits[i];
if (i < 2) {
result += " = ";
}
}
console.log(result);
cont = prompt(result + "\nDo you wish to continue (Y/N)?");
contValue = cont.toUpperCase();
if (contValue === "N") {
break;
}
}