The following files contain code that runs when a function is invoked. Refactor each of these files so that:
- the initial argument values are submitted by the user using form fields;
- the response is printed to
index.html
.
For each of the .js
files in this Gist:
- Create a new HTML file named after the respective
.js
file. For example,captialize.html
should link tocapitalize.js
in a<script>
element in the<head>
; - In this HTML file,
- add an appropriate form field and
<label>
for each of the values the user will be submitting. Examples:- for a
Number
:<label for="sub-total">Sub-Total: </label> <input type="number" name="gst" id="sub-total">
- for a
String
:<label for="sub-total">Full Name:</label> <input type="text" name="full-name" id="full-name">
- for a
- Add a
<button type="submit">
element to submit the data, if it's not already present. - Add a
<p>
element to store the response, if it's not already present.
- add an appropriate form field and
- In the
.js
file:-
Assign form and paragraph to its own variable using
document.querySelector()
:const form = document.querySelector('form'); const output = document.querySelector('.output')
-
Assign supplied function to a
submit
event on the<form>
usingEventTarget.addEventListener()
:form.addEventListener('submit', clickHandler);
clickHandler
can be renamed to the provided function name;
-
Inside the
clickHandler
, assign form field data to the appropriate variable usingform.FormElementId.value
.const subtTotal = form.subtotal.value;
-
Instead of using
console.log
, useNode.textContent
to send theclickHandler
response to a<p>
element (for example) on the.html
page.output.textContent = `some response with ${calculation}`
-
Both the Tip Calculator and the GST Calculator deal with rates and percentages.
- Write a utility function that will convert a rate (i.e.
0.05
) into a percentage (5%
as a string).- Note: you will need to use
return
keyword to pass the new percentage string back to the calling script.
- Note: you will need to use
- Refactor the Tip and GST Calculators to use your function and include the GST/Tip percentage in its response.