As stated above I need this to convert temperatures and show the respective picture but all I get now is NaN
for the resulting conversion.
I have been trying to fix it but can’t seem to find the issue.
Can someone please help?
JavaScript
x
81
81
1
window.addEventListener("DOMContentLoaded", domLoaded);
2
3
function domLoaded() {
4
var convertButton = document.getElementById("convertButton");
5
var cInput = document.getElementById("cInput");
6
var fInput = document.getElementById("fInput");
7
var weatherImage = document.getElementById("weatherImage");
8
hideImage();//hide image initially
9
convertButton.addEventListener("click", convertTemperature);
10
cInput.addEventListener("input", () => {
11
if (fInput.value.length > 0) {
12
fInput.value = "";// to make other input empty when entering value in this
13
}
14
});
15
fInput.addEventListener("input", () => {
16
if (cInput.value.length > 0) {
17
cInput.value = "";// to make other input empty when entering value in this
18
}
19
});
20
21
function hideImage() {
22
weatherImage.style.display = "none";
23
}
24
25
}
26
27
function convertTemperature() {
28
var cInput = document.getElementById("cInput");
29
var fInput = document.getElementById("fInput");
30
var weatherImage = document.getElementById("weatherImage");
31
var errorMessage = document.getElementById("errorMessage");
32
if (cInput.value.length > 0) {// if input not empty
33
if (checkErrorInput(cInput.value)) {// runs while input is valid
34
fInput.value = convertCtoF(parseFloat(cInput.value));
35
showImage(parseFloat(fInput.value));// To show respective gifs
36
}
37
} else if (fInput.value.length > 0) { // if input not empty
38
if (checkErrorInput(fInput.value)) { // runs while input is valid
39
cInput.value = convertFtoC(parseFloat(fInput.value));
40
showImage(parseFloat(fInput.value));// To show respective gifs
41
}
42
} else {
43
errorMessage.innerText = "please enter temperature";
44
}
45
46
function checkErrorInput(input) {
47
if (isNaN(parseFloat(input))) {
48
errorMessage.innerHTML = input + " is not a number";
49
return false; // input is not valid throws error and returns false
50
} else {
51
errorMessage.innerHTML = "";
52
return true; // valid input
53
}
54
}
55
56
function showImage(f) {
57
if (fInput < 32) {
58
weatherImage.src = "cold.png";// set src attribute to cold gif
59
weatherImage.alt = "cold png";
60
} else if (fInput >= 32 && f <= 50) {
61
weatherImage.src = "cool.png";//set src attribute to gif
62
weatherImage.alt = "cool png";
63
} else {
64
weatherImage.src = "warm.png"; //set src attribute to gif
65
weatherImage.alt = "warm png";
66
}
67
weatherImage.style.display = "block";
68
}
69
70
}
71
72
document.addEventListener("DOMContentLoaded", domLoaded);//run when dom content is loaded
73
74
function convertCtoF(degreesCelsius) {
75
return cInput * (9 / 5) + 32;
76
}
77
78
function convertFtoC(degreesFahrenheit) {
79
return (fInput - 32) * 5 / 9;
80
}
81
Any help would be greatly appreciated!
Advertisement
Answer
cInput and fInput are domelements (tags) but, at
JavaScript
1
8
1
function convertCtoF(degreesCelsius) {
2
return cInput * (9 / 5) + 32;
3
}
4
5
function convertFtoC(degreesFahrenheit) {
6
return (fInput - 32) * 5 / 9;
7
}
8
they are used as variables, you need to use cInput.value|fInput.value / use some variable and then assign it wherever you want.
Hope it will rectify yours.