The following files contain code that runs in the global scope of the file on hard-coded variables. Refactor each of these files so that the code is reusable and portable using a pure function.
- Pure function
- A function that takes arguments as the only inputs and returns the result as its only output. Pure functions don't make any changes to variables outside of the function or store internal variables as state (aka. side-effects).
For ONE of the .js
files in this Gist, refactor the following code into a function so that it:
- accepts the variables (defined at the top of the script) as a parameter(s);
- Note: the variable(s) defined at the top of each script should remain outside the declared function.
- returns the result outside of the function using a
return
statement; - logs the returned result to the console from outside the function.
You may need to remove irrelevant <script>
elements in the provided index.html
file.
- Using the
window.prompt()
method, define your arguments based on user input;- Note:
prompt()
will convert numbers to strings. Keep this in mind when performing arithmetic.
- Note:
- Use
Number.prototype.toFixed()
to round numbers as needed. - Once you're done with your chosen
.js
file, repeat the process for the rest of the files.