Skip to content

Instantly share code, notes, and snippets.

@yang-wei
Created February 29, 2016 10:41
Show Gist options
  • Save yang-wei/167a09c863b26d3d31a8 to your computer and use it in GitHub Desktop.
Save yang-wei/167a09c863b26d3d31a8 to your computer and use it in GitHub Desktop.
ClojureScript BMI Calculator
;; -------------------------
;; BMI Calculator
(def bmi-data
(reagent/atom {:height 180
:weight 60}))
(defn slider [min max val param]
[:input {:type "range"
:value val
:min min
:max max
:on-change (fn [e]
(swap! bmi-data assoc param (.-target.value e)))}])
(defn cal-bmi []
(let [{:keys [height, weight] :as data} @bmi-data
h (/ height 100)]
(assoc data :bmi (/ weight (* h h)))))
(defn view []
(let [{:keys [weight, height, bmi]} (cal-bmi)]
[:div
[:h1 "BMI Calculator"]
[:div
"Height :" (int height) "cm"
(slider 120 240 height :height)]
[:div
"Weight :" (int weight) "kg"
(slider 60 140 weight :weight)]
[:div
"BMI :" (int bmi)
(slider 10 50 bmi :bmi)]]))
(defn main []
view)
;; -------------------------
;; Initialize app
(defn mount-root []
(reagent/render [main] (.getElementById js/document "app")))
(defn init! []
(mount-root))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment