Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fabien Lim's Temperature Converter #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
<link rel="stylesheet" href="style.css">
</head>
<body>
<p>temperature converter</p>
<input class="starter" id="input" placeholder="input"/>
<h2>output:</h2>
<form action="/action_page.php">
<label for="unit">Choose a temperature unit:</label>
<select name="unit" id="tempUnit">
<option value="cel">°C</option>
<option value="fan">°F</option>
<option value="kel">K</option>
</select>
</form>
<h2>Input:</h2>
<input class="tempInput" id="input1" placeholder="Temperature" type="number"/>
<input class="nameInput" id="input2" placeholder="Name" type="text"/>
<button type="button" id="btn" value="Submit">Submit</button>
<br>
<h2>Output:</h2>
<p class="starter" id="output"></p>
<script>
document.querySelector('#input').addEventListener('change', function(event){
var currentInput = event.target.value;
var output = inputHappened(currentInput);
document.querySelector('#btn').addEventListener('click', function(event){
var output = buttonClicked();
display( output );
});

Expand All @@ -21,4 +31,4 @@ <h2>output:</h2>
</script>
<script src="script.js"></script>
</body>
</html>
</html>
81 changes: 78 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,82 @@
console.log("hello script js");

var inputHappened = function(currentInput){
console.log( currentInput );
var output = "WOW TEMPERATURE";

function convertFan(currentInput){
console.log(currentInput);
//fan to others
let fan = currentInput;
let fanToCel = (fan - 32) * 5/9;
let fanToKel = (fan - 32) * 5/9 + 273.15;
let output = fan + "°F = " + fanToCel + "°C = " + fanToKel + "K";
return output;
}

function convertCel(currentInput){
console.log(currentInput);
//cel to others
let cel = currentInput;
let celToFan = (cel * 9/5) + 32;
let celToKel = (cel * 1) + 273.15;
let output = cel + "°C = " + celToFan + "°F = " + celToKel + "K";
return output;
}

function convertKel(currentInput){
console.log(currentInput);
//kel to others
let kel = currentInput;
let kelToCel = kel - 273.15;
let kelToFan = (kel - 273.15) * 9/5 + 32;
let output = kel + "K = " + kelToCel + "°C = " + kelToFan + "°F";
return output;
}

function buttonClicked(){
var currentInput = document.getElementById('input1').value;
console.log(currentInput);
var name = document.getElementById('input2').value;
console.log(name);
var tempUnit = document.getElementById('tempUnit').value;
console.log(tempUnit);
var originalTemp = parseFloat(currentInput);
if (tempUnit === "cel"){
if (originalTemp <= 0){
displayMsg("it is so cold, " + name + " please wear something warm");
} else if (originalTemp > 0 && originalTemp < 40){
displayMsg("it is normal, " + name + " please wear something");
} else if (originalTemp >= 40 && originalTemp < 100){
displayMsg("it is so hot, " + name + " please wear something cooling");
} else if (originalTemp >= 100){
displayMsg("it is boiling, " + name + " please do not go out at all");
}
return convertCel(originalTemp);
} else if (tempUnit === "fan"){
if (originalTemp <= 32){
displayMsg("it is so cold, " + name + " please wear something warm");
} else if (originalTemp > 32 && originalTemp < 104){
displayMsg("it is normal, " + name + " please wear something");
} else if (originalTemp >= 104 && originalTemp < 210.2){
displayMsg("it is so hot, " + name + " please wear something cooling");
} else if (originalTemp >= 210.2){
displayMsg("it is boiling, " + name + " please do not go out at all");
}
return convertFan(originalTemp);
} else if (tempUnit === "kel"){
if (originalTemp <= 273.15){
displayMsg("it is so cold, " + name + " please wear something warm");
} else if (originalTemp > 273.15 && originalTemp < 313.15){
displayMsg("it is normal, " + name + " please wear something");
} else if (originalTemp >= 313.15 && originalTemp < 373.15){
displayMsg("it is so hot, " + name + " please wear something cooling");
} else if (originalTemp >= 373.15){
displayMsg("it is boiling, " + name + " please do not go out at all");
}
return convertKel(originalTemp);
}
};

function displayMsg(msg){
const msgText = document.createElement('p');
msgText.innerText = msg;
document.body.appendChild(msgText);
};
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

#output{
background-color:pink;
}
}