Improve temperature conversion logic
- Use single variable for initial temperature - Allow command to accept "to" keyword
This commit is contained in:
parent
eee9539ac2
commit
b81d675e4c
1 changed files with 13 additions and 5 deletions
|
@ -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);
|
||||
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue