Handle negative numbers with an error message, fix indentation
authorNick Downing <nick@ndcode.org>
Sun, 25 Nov 2018 02:41:28 +0000 (13:41 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 25 Nov 2018 02:41:28 +0000 (13:41 +1100)
calculatebutton.html.jst

index e47ad2c..06080b0 100644 (file)
@@ -15,16 +15,32 @@ return async env => {
     let query = querystring.parse((await data).toString())
 
     let radius_and_units = query.radius.split(' ')
-
-let radius = parseFloat(radius_and_units[0])
-let units = radius_and_units[1] || 'units'
-let area = Math.PI * radius * radius
-let circumference = Math.PI * radius * 2
-let diameter = radius * 2
-
-    _site.serve(
-      env,
-      200,
+    let radius = parseFloat(radius_and_units[0])
+    let units = radius_and_units[1] || 'units'
+    let area = Math.PI * radius * radius
+    let circumference = Math.PI * radius * 2
+    let diameter = radius * 2
+    if (radius < 0) {
+      _site.serve(
+        env,
+        200,
+        `<!DOCTYPE html>
+<html>
+<head>
+<title>Hello World HTML</title>
+</head>
+<body>
+<h1>Error - Unable to Calculate Negative Radius</h1>
+</body>
+</html>
+`,
+        'calculatebutton.html.jst'
+      )
+    }
+    else {
+      _site.serve(
+        env,
+        200,
       `<!DOCTYPE html>
 <html>
 <head>
@@ -38,8 +54,9 @@ let diameter = radius * 2
 </body>
 </html>
 `,
-      'calculatebutton.html.jst'
-    )
+        'calculatebutton.html.jst'
+      )
+    }
   }
   else {
     _site.serve(
@@ -51,7 +68,7 @@ let diameter = radius * 2
 <title>Hello World HTML</title>
 </head>
 <body>
-<h1>404 Error - Unable to Calculate</h1>
+<h1>Error - Unable to Calculate</h1>
 </body>
 </html>
 `,