Completing the Calculation
Now that we know we can get to an element and detect a click, we can expand the calculate function to:
-
Retrieve the values from the quantity and shirt size fields—these will be option(string) values.
-
Convert the quantity to option(int).
-
Calculate the price, yielding an option(float).
-
Convert the result to a string (the empty string if we have a None result).
-
Put the string into the <span id="price">.
There are steps where things could go wrong: we might not be able to find the elements for the fields or convert the quantity string to an integer. This means we need option variables throughout, and this is where many JavaScript programmers rebel at the cognitive burden of figuring out which Belt.Option functions to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access