Created
February 29, 2016 10:41
-
-
Save yang-wei/167a09c863b26d3d31a8 to your computer and use it in GitHub Desktop.
ClojureScript BMI Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; ------------------------- | |
;; 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