Skip to content

Commit

Permalink
Temperature conversion exercise (#14)
Browse files Browse the repository at this point in the history
* Adding new temperature conversion exercise with model answer.

* Minor tidy-ups.

---------

Co-authored-by: atg103 <7151354+atg103@users.noreply.github.com>
  • Loading branch information
atg103 and atg103 authored Sep 20, 2024
1 parent dcdc9ec commit 2df90b9
Show file tree
Hide file tree
Showing 17 changed files with 7,345 additions and 0 deletions.
1 change: 1 addition & 0 deletions live-assignments/temperature-converter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions live-assignments/temperature-converter/answer/app/css/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
body {
font-family: 'Arial', 'Tahoma', 'sans-serif';
}
#form {
width: 466px;
text-align: right;
margin-bottom: 4px;
}
label {
display: inline-block;
margin-bottom: 2px;
}
#output {
border: 1px solid #999;
display: block;
width: 456px;
height: 25px;
padding: 4px;
font-size: 11pt;
}
22 changes: 22 additions & 0 deletions live-assignments/temperature-converter/answer/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en_GB">
<head>
<meta charset="utf-8" />
<title>Temperature Converter</title>
<link rel="stylesheet" type="text/css" href="/css/global.css" />
<script type="text/javascript" src="/js/tempconv.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
<script type="text/javascript">
window.addEventListener("load", App.initialise);
</script>
</head>
<body>
<div id="form">
<label>From: <select type="menu" id="fromTemp"><option value="C">Celcius</option><option value="F">Farenheit</option><option value="K">Kelvin</option></select></label>
<label>To: <select type="menu" id="toTemp"><option value="C">Celcius</option><option value="F">Farenheit</option><option value="K">Kelvin</option></select></label>
<label>Value: <input type="text" id="tempValue" placeholder="Enter a number" /></label>
<button id="submit">Convert</button>
</div>
<section id="output"></section>
</body>
</html>
31 changes: 31 additions & 0 deletions live-assignments/temperature-converter/answer/app/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
App = (function() {
let temperatureConverter = new TemperatureConverter();

let setOutput = function(text) {
document.querySelector("#output").innerText = text;
};

let displayTemperatureUnits = function(tempCode) {
return (tempCode == 'F' || tempCode == 'C') ? '°' + tempCode : tempCode;
};

let performConversionAndShowResult = function(fromTempCode, toTempCode, value) {
let converted = temperatureConverter.convert(fromTempCode, toTempCode, value);
setOutput(value + displayTemperatureUnits(fromTempCode)
+ " is equivalent to " + converted + displayTemperatureUnits(toTempCode));
};

return {
initialise: function() {
let fromTempControl = document.getElementById("fromTemp");
let toTempControl = document.getElementById("toTemp");
let valueControl = document.getElementById("tempValue");

let submitControl = document.querySelector('#submit');
submitControl.onclick = function() {
performConversionAndShowResult(fromTempControl.value, toTempControl.value, Number(valueControl.value));
};
console.log("Application initialised.");
}
};
})();
45 changes: 45 additions & 0 deletions live-assignments/temperature-converter/answer/app/js/tempconv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function TemperatureConverter() {
this.supportedConversions = {
C: {
F: function(val) { return val * 9 / 5 + 32; },
K: function(val) { return val + 273; }
},
F: {
C: function(val) { return (val-32) * 5 / 9; },
K: function(val) { return (val-32) * 5 / 9 + 273; }
},
K: {
F: function(val) { return (val-273) * 9 / 5 + 32; },
C: function(val) { return val - 273; }
}
};
}

TemperatureConverter.prototype.lookupConversionFor = function(fromTemp, toTemp) {
try {
var conversionFunction = this.supportedConversions[fromTemp][toTemp];
if (typeof(conversionFunction) === 'function') {
return conversionFunction;
}
} catch (typeError) { }

console.error("No supported conversion from:" + fromTemp + " to " + toTemp);
return function(value) { /* no-op */ };
};

TemperatureConverter.prototype.convert = function(fromTemp, toTemp, value) {
if (fromTemp == null || toTemp == null || value == null) {
return undefined;
}
if (fromTemp == "K" && value < 0) {
return undefined;
}
if (fromTemp == toTemp) {
return value;
}
return this.lookupConversionFor(fromTemp, toTemp)(value);
};

if (module) {
module.exports = TemperatureConverter;
}
Loading

0 comments on commit 2df90b9

Please sign in to comment.