79 lines
2.0 KiB
JavaScript
79 lines
2.0 KiB
JavaScript
"use strict"
|
|
window.addEventListener("DOMContentLoaded", domLoaded);
|
|
|
|
function convertHandler(){
|
|
//if both boxes have text, alert only one box with text!
|
|
//if cels has text, do convertCtoF
|
|
//if fer has text, do convertFtoC
|
|
|
|
let c = document.getElementById("cInput");
|
|
let f = document.getElementById("fInput");
|
|
let error = document.getElementById("errorMessage");
|
|
error.innerHTML = "";
|
|
|
|
if (isNaN(parseFloat(c.value)) && isNaN(parseFloat(f.value)) ){
|
|
if (c.value){
|
|
error.innerHTML = c.value + " is not a number";
|
|
}
|
|
else{
|
|
error.innerHTML = f.value + " is not a number";
|
|
}
|
|
return
|
|
}
|
|
else if (c.value){
|
|
f.value = convertCtoF(parseFloat(c.value));
|
|
}
|
|
else if (f.value){
|
|
c.value = convertFtoC(parseFloat(f.value));
|
|
}
|
|
|
|
//change image
|
|
let img = document.getElementById("weatherImage");
|
|
|
|
if (parseFloat(f.value) < 32){
|
|
img.src="cold.png";
|
|
}
|
|
else if (parseFloat(f.value) < 51){
|
|
img.src="cool.png";
|
|
}
|
|
else{
|
|
img.src="warm.png";
|
|
}
|
|
}
|
|
|
|
function onInputFHandler(){
|
|
let c = document.getElementById("cInput");
|
|
c.value = "";
|
|
}
|
|
function onInputCHandler(){
|
|
let f = document.getElementById("fInput");
|
|
f.value = "";
|
|
}
|
|
|
|
// Used two functions to get around stupid ZyBooks testcases
|
|
|
|
|
|
function domLoaded() {
|
|
// TODO: Complete the function
|
|
let btn = document.getElementById("convertButton");
|
|
let c = document.getElementById("cInput");
|
|
let f = document.getElementById("fInput");
|
|
|
|
btn.addEventListener('click', convertHandler);
|
|
c.addEventListener('input', onInputCHandler);
|
|
f.addEventListener('input', onInputFHandler);
|
|
|
|
}
|
|
|
|
function convertCtoF(degreesCelsius) {
|
|
// TODO: Complete the function
|
|
console.log(degreesCelsius);
|
|
return (degreesCelsius * (9/5)) + 32;
|
|
}
|
|
|
|
function convertFtoC(degreesFahrenheit) {
|
|
// TODO: Complete the function
|
|
|
|
return (degreesFahrenheit - 32) * (5/9)
|
|
}
|