Skip to content

Commit e3936a8

Browse files
committed
type conversion using parseInt()
1 parent 539d6e8 commit e3936a8

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

Temperature Convertor/script.js

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,86 @@
1-
let results = document.getElementById('converted-value')
1+
let results = document.getElementById("converted-value");
22

33
// On change event for the second SELECT attribute
44
to.onchange = () => {
5-
results.innerHTML = ""
6-
results.style.color = 'black'
7-
8-
let from = document.getElementById('from')
9-
let to = document.getElementById('to')
10-
let input = document.getElementById('input').value
11-
12-
let fromValue = from.value
13-
let toValue = to.value
14-
15-
// Checking if both are same
16-
if(fromValue === toValue) {
17-
results.style.color = 'red'
18-
results.innerHTML = "CONVERSION UNITS SHOULD BE DIFFERENT!"
19-
}
20-
21-
// Checking the input and passing it to appropriate functions
22-
if(fromValue === 'celsius' && toValue === 'kelvin') {
23-
celsiusToKelvin(input)
24-
}
25-
26-
if(fromValue === 'celsius' && toValue === 'fahrenheit') {
27-
celsiusToFahrenheit(input)
28-
}
29-
30-
if(fromValue === 'fahrenheit' && toValue === 'kelvin') {
31-
fahrenheitToKelvin(input)
32-
}
33-
34-
if(fromValue === 'fahrenheit' && toValue === 'celsius') {
35-
fahrenheitToCelsius(input)
36-
}
37-
38-
if(fromValue === 'kelvin' && toValue === 'fahrenheit') {
39-
KelvinToFahrenheit(input)
40-
}
41-
42-
if(fromValue === 'kelvin' && toValue === 'celsius') {
43-
KelvinToCelsius(input)
44-
}
45-
}
5+
results.innerHTML = "";
6+
results.style.color = "black";
467

8+
let from = document.getElementById("from");
9+
let to = document.getElementById("to");
10+
let input = parseInt(document.getElementById("input").value, 10);
11+
12+
let fromValue = from.value;
13+
let toValue = to.value;
14+
15+
// Checking if both are same
16+
if (fromValue === toValue) {
17+
results.style.color = "red";
18+
results.innerHTML = "CONVERSION UNITS SHOULD BE DIFFERENT!";
19+
}
20+
21+
// Checking the input and passing it to appropriate functions
22+
if (fromValue === "celsius" && toValue === "kelvin") {
23+
celsiusToKelvin(input);
24+
}
25+
26+
if (fromValue === "celsius" && toValue === "fahrenheit") {
27+
celsiusToFahrenheit(input);
28+
}
29+
30+
if (fromValue === "fahrenheit" && toValue === "kelvin") {
31+
fahrenheitToKelvin(input);
32+
}
33+
34+
if (fromValue === "fahrenheit" && toValue === "celsius") {
35+
fahrenheitToCelsius(input);
36+
}
37+
38+
if (fromValue === "kelvin" && toValue === "fahrenheit") {
39+
KelvinToFahrenheit(input);
40+
}
41+
42+
if (fromValue === "kelvin" && toValue === "celsius") {
43+
KelvinToCelsius(input);
44+
}
45+
};
4746

4847
// Conversion of input values
48+
4949
const celsiusToKelvin = (input) => {
50-
results.innerHTML = `The final converted value is ${parseInt(input + 273.15, 10).toFixed(2)}`
51-
}
50+
results.innerHTML = `The final converted value is ${(input + 273.15).toFixed(
51+
2
52+
)}`;
53+
};
5254

5355
const celsiusToFahrenheit = (input) => {
54-
results.innerHTML = `The final converted value is ${parseInt(input * (9 / 5) + 32, 10).toFixed(2)}`
55-
}
56-
56+
results.innerHTML = `The final converted value is ${(
57+
input * (9 / 5) +
58+
32
59+
).toFixed(2)}`;
60+
};
5761
const fahrenheitToKelvin = (input) => {
58-
results.innerHTML = `The final converted value is ${parseInt((input + 459.67) * (5 / 9), 10).toFixed(2)}`
59-
}
62+
results.innerHTML = `The final converted value is ${(
63+
(input + 459.67) *
64+
(5 / 9)
65+
).toFixed(2)}`;
66+
};
6067

6168
const fahrenheitToCelsius = (input) => {
62-
results.innerHTML = `The final converted value is ${parseInt((input - 32) * (5 / 9), 10).toFixed(2)}`
63-
}
69+
results.innerHTML = `The final converted value is ${(
70+
(input - 32) *
71+
(5 / 9)
72+
).toFixed(2)}`;
73+
};
6474

6575
const KelvinToFahrenheit = (input) => {
66-
results.innerHTML = `The final converted value is ${parseInt((input * 1.8) - 459.67, 10).toFixed(2)}`
67-
}
76+
results.innerHTML = `The final converted value is ${(
77+
input * 1.8 -
78+
459.67
79+
).toFixed(2)}`;
80+
};
6881

6982
const KelvinToCelsius = (input) => {
70-
results.innerHTML = `The final converted value is ${parseInt(input - 273.15, 10).toFixed(2)}`
71-
}
83+
results.innerHTML = `The final converted value is ${(input - 273.15).toFixed(
84+
2
85+
)}`;
86+
};

0 commit comments

Comments
 (0)