# nathansmith/javascript-quiz

Renaming `sum` to `add`. No functional changes.

1 parent ec64cc9 commit 1d30646c7a8a416d9f325cfcf926b9cfcd6ba6c6 committed Nov 27, 2012
Showing with 11 additions and 11 deletions.
1. +6 −6 ANSWERS.md
2. +5 −5 README.md
 @@ -627,28 +627,28 @@ Joking aside though, if you're a job applicant skimming these answers so that yo })(this.document); ``` -21. Write a function named `sum` that returns the total of any number of parameters. Example… +21. Write a function named `add` that returns the total of any number of parameters. Example… ```js // Should equal 15 - sum(1, 2, 3, 4, 5); + add(1, 2, 3, 4, 5); // Should equal 0 - sum(5, null, -5); + add(5, null, -5); // Should equal 10 - sum('1.0', false, 1, true, 1, 'A', 1, 'B', 1, 'C', 1, 'D', 1, 'E', 1, 'F', 1, 'G', 1); + add('1.0', false, 1, true, 1, 'A', 1, 'B', 1, 'C', 1, 'D', 1, 'E', 1, 'F', 1, 'G', 1); // Should equal 0.3, not 0.30000000000000004 - sum(0.1, 0.2); + add(0.1, 0.2); ``` **Answer:** To handle an arbitrary number of parameters passed into a function, JavaScript gives us the handy, built-in variable called `arguments`. Here, we can use `parseFloat`, to ensure we filter out any non-numeric values that might be passed in, while still allowing string representations of numbers to be converted. Also, values are multiplied by `1e12`, totaled, then divided by `1e12`, to account for any decimal oddities due to floating-point arithmetic. ```js - function sum() { + function add() { // Start from zero var total = 0;
 @@ -106,18 +106,18 @@ The intent is to weed out some "Just use jQuery" applicants, but allow those tha var my_number = my_string - 0; ``` -21. Write a function named `sum` that returns the total of any number of parameters. Example… +21. Write a function named `add` that returns the total of any number of parameters. Example… ```js // Should equal 15 - sum(1, 2, 3, 4, 5); + add(1, 2, 3, 4, 5); // Should equal 0 - sum(5, null, -5); + add(5, null, -5); // Should equal 10 - sum('1.0', false, 1, true, 1, 'A', 1, 'B', 1, 'C', 1, 'D', 1, 'E', 1, 'F', 1, 'G', 1); + add('1.0', false, 1, true, 1, 'A', 1, 'B', 1, 'C', 1, 'D', 1, 'E', 1, 'F', 1, 'G', 1); // Should equal 0.3, not 0.30000000000000004 - sum(0.1, 0.2); + add(0.1, 0.2); ```