Last active
June 30, 2017 17:38
-
-
Save ideaflare/3fbb693995fff169fd61b202dd481609 to your computer and use it in GitHub Desktop.
Check that all arguments are present: function unspecified(args) {..}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <p>Finding the sum of numbers.</p> | |
| <p>Sum() returns <span id="demo-no-arguments"></span></p> | |
| <p>Sum(32) returns <span id="demo-32"></span></p> | |
| <p>Sum(1,2,3,4,5) returns <span id="demo-1-to-5"></span></p> | |
| <script> | |
| function unspecified(args) { | |
| return args.reduce((all,next) => | |
| all && (undefined === next), true); | |
| } | |
| function sum(a) { | |
| if(unspecified([...arguments])){ | |
| return 0; | |
| } | |
| return [...arguments].reduce((total,next) => | |
| total + next, 0); | |
| } | |
| document.getElementById("demo-no-arguments").innerHTML = sum(); | |
| document.getElementById("demo-32").innerHTML = sum(32); | |
| document.getElementById("demo-1-to-5").innerHTML = sum(1,2,3,4,5); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment