From b81d675e4c39cb4ba29286b61c955c55d016826b Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Fri, 4 Oct 2024 04:25:17 +0200 Subject: [PATCH] Improve temperature conversion logic - Use single variable for initial temperature - Allow command to accept "to" keyword --- commands/misc/convert.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/commands/misc/convert.js b/commands/misc/convert.js index 723697d..bb5b661 100644 --- a/commands/misc/convert.js +++ b/commands/misc/convert.js @@ -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); } -}; \ No newline at end of file +};