-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Temperature conversion exercise (#14)
* Adding new temperature conversion exercise with model answer. * Minor tidy-ups. --------- Co-authored-by: atg103 <7151354+atg103@users.noreply.github.com>
- Loading branch information
Showing
17 changed files
with
7,345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
20 changes: 20 additions & 0 deletions
20
live-assignments/temperature-converter/answer/app/css/global.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
live-assignments/temperature-converter/answer/app/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
live-assignments/temperature-converter/answer/app/js/app.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
live-assignments/temperature-converter/answer/app/js/tempconv.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.