使用JavaScript的温度转换器
我们需要编写一个JavaScript函数,该函数接受一个表示摄氏或华氏温度的字符串。
像这样-
"23F", "43C", "23F"
我们需要编写一个JavaScript函数,该函数接受此字符串并将温度从摄氏温度转换为华氏度,并将华氏温度转换为摄氏温度。
示例
以下是代码-
const temp1 = '37C';
const temp2 = '100F';
const tempConverter = temp => {
const degree = temp[temp.length-1];
let converted;
if(degree === "C") {
converted = (parseInt(temp) * 9 / 5 + 32).toFixed(2) + "F";
}else {
converted = ((parseInt(temp) -32) * 5 / 9).toFixed(2) + "C";
};
return converted;
};
console.log(tempConverter(temp1));
console.log(tempConverter(temp2));输出结果
以下是控制台中的输出-
98.60F 37.78C
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短