EXERCISE: Converting numbers to positions continued

Convert numbers to positions

Extend the positions function to include 2nd and 3rd

(defn position [number]
  (if ;; condition
      ;; then
      ;; else
(position 2)


if expressions can be included inside another if expression

Reveal answer...
(defn position [number]
  (if (= 1 number)
    (str number "st")
    (if (= 2 number)
      (str number "nd")
      (if (= 3 number)
        (str number "rd")
        (str number "th")))))

(position 2)

There is a better way..

Nesting if statements is not the preferred way to write code, so we will see alternative functions next

results matching ""

    No results matching ""