Skip to content

Commit 0a77e97

Browse files
authored
Update Convert Celsius to Fahrenheit.js
combine the conversions
1 parent c8ad48a commit 0a77e97

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
/*
22
activation_example:30c or 30 degrees celsius
3-
regex:(?:^|\s)(-?\d{1,3}\.?\d{0,2})°?\s?(?:degrees)?\s?c(?:elsius|elcius)?\b
3+
regex:(?:^|\s)(-?\d{1,3}\.?\d{0,2})°?\s?(?:degrees)?\s?(?:c(?:elsius)?|f(?:ahrenheit)?)\b
44
flags:gmi
55
*/
66

7-
const regexTest = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?c(?:elsius)?\b/gi;
8-
const celsiusToFahrenheit = c => ((c * 9 / 5) + 32).toFixed(2);
97
const formatNumber = num => Number(num).toFixed(2).replace(/\.00$/, '');
108
const conversions = [];
119

12-
let match;
13-
while ((match = regexTest.exec(current.text)) !== null) {
14-
const celsius = parseFloat(match);
10+
// convert C to F
11+
const celsiusRegex = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?c(?:elsius)?\b/gi;
12+
const celsiusToFahrenheit = c => ((c * 9 / 5) + 32).toFixed(2);
13+
let cMatch;
14+
while ((cMatch = celsiusRegex.exec(current.text)) !== null) {
15+
const celsius = parseFloat(cMatch);
1516
const fahrenheit = celsiusToFahrenheit(celsius);
16-
conversions.push(`${formatNumber(celsius)}°C is ${formatNumber(fahrenheit)} degrees in sane units (Fahrenheit).`);
17+
conversions.push(`${formatNumber(celsius)}°C is ${formatNumber(fahrenheit)} degrees in freedom units (Fahrenheit).`);
1718
}
1819

20+
// convert F to C
21+
const fahrenheitRegex = /(-?\d{1,3}(?:\.\d{1,2})?)°?\s?(?:degrees?)?\s?f(?:ahrenheit)?\b/gi;
22+
const fahrenheitToCelsius = f => ((f - 32) * 5 / 9).toFixed(2);
23+
let fMatch;
24+
while ((fMatch = fahrenheitRegex.exec(current.text)) !== null) {
25+
const fahrenheit = parseFloat(fMatch);
26+
const celsius = fahrenheitToCelsius(fahrenheit);
27+
conversions.push(`${formatNumber(fahrenheit)}°F is ${formatNumber(celsius)} degrees in sane units (Celsius).`);
28+
}a
29+
1930
const conversionMessage = conversions.join('\n');
2031

2132
new x_snc_slackerbot.Slacker().send_chat(current, conversionMessage);

0 commit comments

Comments
 (0)