Improve temperature conversion logic

- Use single variable for initial temperature
- Allow command to accept "to" keyword
This commit is contained in:
SileNce5k 2024-10-04 04:25:17 +02:00
parent eee9539ac2
commit b81d675e4c
Signed by: SileNce
GPG key ID: B0A142BB4291B204

View file

@ -10,20 +10,28 @@ module.exports = {
message.channel.send("Not enough arguments");
return;
}
if(args[2] === "to" && args.length > 3){
args[2] = args[3];
}
let sendText = "";
let initial_temp = parseFloat(args[0])
switch (args[1].toUpperCase()) {
case "C":
if (args[2].toUpperCase() === "F") {
const fahrenheit = ((parseFloat(args[0]) * 9 / 5) + 32).toFixed(2);
sendText = `${args[0]}°C is ${fahrenheit}°F`;
let fahrenheit = ((initial_temp * 9 / 5) + 32).toFixed(2);
if(fahrenheit[fahrenheit.length - 1] === '0' && fahrenheit[fahrenheit.length - 2] === "0"){
fahrenheit = fahrenheit.replace(".00","")
}
sendText = `${initial_temp}°C is ${fahrenheit}°F`;
} else {
sendText = "Can only convert to celsius from fahrenheit";
}
break;
case "F":
if (args[2].toUpperCase() === "C") {
const celsius = ((parseFloat(args[0]) - 32) * 5 / 9).toFixed(2);
sendText = `${args[0]}°F is ${celsius}°C`;
const celsius = ((initial_temp - 32) * 5 / 9).toFixed(2);
sendText = `${initial_temp}°F is ${celsius}°C`;
} else {
sendText = "Can only convert to fahrenheit from celsius";
}
@ -35,4 +43,4 @@ module.exports = {
message.channel.send(sendText);
}
};
};