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
117 lines (89 loc) · 3.96 KB
/
script.js
File metadata and controls
117 lines (89 loc) · 3.96 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
//Part 1
//var temperatureInFahr = prompt ("Please enter temperature in Farenheit.");
//var temperatureInKelvin = temperatureInFahr-459.67;
//var temperatureInCelsius = temperatureInFahr+32;
//var temperatureInFahr = temperatureInCelsius-17.7778;
//Part 2
var userName = prompt("Please enter your name.")
var temperature = prompt("Please enter temperature.");
/*if(typeof temperature !== 'number'){
alert("Invalid input!");
prompt("Please enter temperature.");
temperature = userInput.nextInt();
}*/
var question = "Please enter temperature unit (Kelvin, Celsius, Farenheit)."
var temperatureUnit = prompt(question).toLowerCase();
//&& (temperatureUnit == "kelvin"|| temperatureUnit == "celsius"|| temperatureUnit =="farenheit")) {
if (temperatureUnit === "celsius"){
var temperatureInKelvin = temperature+273.15;
var temperatureInFahr = (temperature*1.8)+32;
alert(temperature + " degC = " + temperatureInKelvin + " K = " + temperatureInFahr + " degF");
} else if (temperatureUnit=== "kelvin"){
var temperatureInCelsius = temperature-273.15;
var temperatureInFahr = (temperature*1.8)-459.67;
alert(temperature+" K = " + temperatureInCelsius + " degC = " + temperatureInFahr + " degF");
} else if (temperatureUnit ==="farenheit"){
var temperatureInKelvin = (temperature+459.67)*5/9;
var temperatureInCelsius = (temperature-32)*5/9;
alert(temperature + " degF = " + temperatureInKelvin + " K = " + temperatureInCelsius + " degC");
}
//Part 3
//Some additional tryout stuff
//var tempCelsius = [temperature, allUnits.temperatureInFahr[0], allUnits.temperatureInKelvin[0]];
//var tempKelvin = [temperature, temperatureInCelsius, temperatureInFahr];
//var tempFahr = [temperature, temperatureInKelvin, temperatureInCelsius];
var allUnits =
{
temperatureIn: [{
unit: "celsius",
fahr: (temperature*1.8)+32,
kelvin: temperature+273.15
},
{ unit: "kelvin",
fahr: (temperature*1.8)-459.67,
celsius: temperature-273.15
},
{unit: "farenheit",
kelvin: (temperature+459.67)*5/9,
celsius: (temperature-32)*5/9
}]
};
var converted_temp_1;
var converted_temp_2;
var temps = [temperature, converted_temp_1, converted_temp_2];
if (temperatureUnit === "celsius") {
temps[1] = allUnits.temperatureIn[0].fahr;
temps[2] = allUnits.temperatureIn[0].kelvin;
console.log(temps);
}
else if (temperatureUnit === "kelvin") {
temps[1] = allUnits.temperatureIn[1].fahr;
temps[2] = allUnits.temperatureIn[1].celsius;
console.log(temps);
}
else if (temperatureUnit === "farenheit") {
temps[1] = allUnits.temperatureIn[2].kelvin;
temps[2] = allUnits.temperatureIn[2].celsius;
console.log(temps);
}
//Part 5
var clothesCold = ['a scarf', 'a coat', 'leg warmers', 'a sweater'];
var arrCold = clothesCold[Math.floor(Math.random()*clothesCold.length)];
var clothesHot = ['less', 'a cap', 'shorts', 'a singlet'];
var arrHot = clothesHot[Math.floor(Math.random()*clothesHot.length)];
var clothesMelt = ['nothing', 'swimwear', 'an ice pack'];
var arrMelt = clothesMelt[Math.floor(Math.random()*clothesMelt.length)];
//Part 4
if ((temperatureUnit === "celsius" && temps[0]<0) || (temperatureUnit === "kelvin" && temps[2]<0 )|| (temperatureUnit ==="farenheit" && temps[2]<0)){
console.log(`Hello ${userName}! Brrr it's cold outside! Please wear ${arrCold}.`);
} else if ((temperatureUnit === "kelvin" && temps[2]>40)||(temperatureUnit ==="celsius" && temps[0]>40)|| (temperatureUnit ==="farenheit" && temps[2]>40)) {
console.log(`Hello ${userName}! Woah it's hot, man! Please wear ${arrHot}.`);
}else if ((temperatureUnit === "farenheit" && temps[2]>100)||(temperatureUnit ==="celsius" && temps[0]>100)||(temperatureUnit === "kelvin" && temps[2]>100)){
console.log(`Hello ${userName}! You will melt! Please wear ${arrMelt}.`);
}
//Part 6
var arrUnits = ["°C", "K", "°F"];
for(var i=0; i<temps.length; i++){
console.log("Temperature: " + temps[i] + " " + arrUnits[i]);
}
//Part 7