Skip to content

Commit 53bca67

Browse files
committed
feat(temperature) : add the logic for calculation
1 parent 6633b82 commit 53bca67

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

Temperature-convertor/index.html

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ <h1>Temperature convertor</h1>
1313
<form action="" method="post" id="form">
1414
<h2 >Type of conversion</h2>
1515
<div class="btn-section">
16-
<button type="button" onclick="getSentenceResult();">Celsius</button>
17-
<button type="button" onclick="getSentenceResult();">Fahrenheit </button>
18-
16+
<button type="button" onclick="getTypeTemperatureInput('Celsius');">Celsius</button>
17+
<button type="button" onclick="getTypeTemperatureInput('Fahrenheit');">Fahrenheit </button>
1918
</div>
2019
<div class="input-section">
2120
<label for="response">Temperature</label>
2221
<input type="text" name="response" id="response" placeholder="12">
2322
</div>
2423
<button type="button" onclick="getSentenceResult();">Submit</button>
24+
<section id="element_result" style="font-size: larger;">
25+
26+
</section>
2527
</form>
26-
<section id="element_result" style="font-size: larger;">
27-
28-
</section>
2928
</body>
3029
<script src="./main.js"></script>
3130
</html>

Temperature-convertor/main.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ const form = document.getElementById('form');
55

66
const element_result = document.getElementById('element_result');
77
let sentence = '';
8+
let temperatureInput = '';
9+
function getTypeTemperatureInput(ipt){
10+
return temperatureInput = ipt;
11+
}
812
function transformCelsiusToFahrenheit(degCel){
913
return (degCel * (9/5) + 32);
1014
}
15+
function transformFahrenheitToCelsius(degCel){
16+
return (degCel - 32 * 5/9);
17+
}
1118
form.onkeydown = function(e){
1219
if(e.keyCode == 13){
1320
e.preventDefault();
@@ -16,10 +23,14 @@ form.onkeydown = function(e){
1623
};
1724
function getSentenceResult(){
1825
const value = +res.value;
19-
26+
if(temperatureInput === ''){
27+
return element_result.innerHTML = 'Please enter valid conversion';
28+
}
2029
if(isNaN(value)){
2130
return element_result.innerHTML = 'Input data not correct.';
2231
}
23-
const val = transformCelsiusToFahrenheit(value);
24-
return element_result.innerHTML = value + ' degrés Celsius donne ' + parseInt(val) + ' Fahrenheit ';
32+
const val = temperatureInput === 'Celsius' ? transformCelsiusToFahrenheit(value) : transformFahrenheitToCelsius(value);
33+
const labelTemperature = (temperatureInput === 'Celsius') ? ' Fahrenheit' : ' Celsius' ;
34+
return element_result.innerHTML = `<p>${value} ${temperatureInput} give ${parseInt(val)} ${labelTemperature}</p>`;
35+
2536
}

Temperature-convertor/style.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ input{
2929
:focus{
3030
box-shadow: rgb(0 0 0 / 0%) 0px 0px 0px 0px, rgb(58 151 212 / 36%) 0px 0px 0px 4px, rgb(0 0 0 / 0%) 0px 0px 0px 0px, rgb(60 66 87 / 16%) 0px 0px 0px 1px, rgb(0 0 0 / 0%) 0px 0px 0px 0px, rgb(0 0 0 / 0%) 0px 0px 0px 0px, rgb(0 0 0 / 0%) 0px 0px 0px 0px;
3131
}
32-
32+
h1,h2{
33+
color: #86b0ff;
34+
text-align: center;
35+
}
3336
button{
3437
display: inline-block;
3538
outline: none;

0 commit comments

Comments
 (0)