I try to output the result in format of JSON. But got error "celsiusObject is not defined".
Part of my code:
const select = document.getElementById('select');
switch (select.value) {
case 'celsius':
value1.value = Math.round(9/5 * (parseInt(currentValue.value)) + 32) + 'F';
value2.value = Math.round(parseInt(currentValue.value) + 273.15) + 'K';
json.value = JSON.stringify(celsiusObject);
const celsiusObject = {
F: Math.round(9/5 * (parseInt(currentValue.value)) + 32),
K: Math.round(parseInt(currentValue.value) + 273.15)
}
};
HTML:
<div id="application">
<input id="currentValue" type="number" placeholder="enter value of temperature">
<select id="select">
<option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
<option value="kelvin">Kelvin</option>
</select>
</br>
<button id="convert">Convert</button>
</br>
<input id="value1" type="text">
</br>
<input id="value2" type="text">
</br>
<input id="value3" type="text">
</div>
What do I wrong to get JSON?
You're getting that because you're using the constant before declaring it, instead, your code should look like this:
const select = document.getElementById('select');
switch (select.value) {
case 'celsius':
value1.value = Math.round(9/5 * (parseInt(currentValue.value)) + 32) + 'F';
value2.value = Math.round(parseInt(currentValue.value) + 273.15) + 'K';
const celsiusObject = {
F: Math.round(9/5 * (parseInt(currentValue.value)) + 32),
K: Math.round(parseInt(currentValue.value) + 273.15)
}
json.value = JSON.stringify(celsiusObject);
};
您必须在JSON.stringify(celsiusObject)之上定义celsiusObject;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.