-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm19.html
42 lines (30 loc) · 978 Bytes
/
algorithm19.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html>
<body>
<script>
//https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional
//Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.
function addTogether(...args) {
if (args.some(value => typeof(value) !== "number")) {
return undefined;
}
if(args.length >1) {
return args.reduce((a,b) => (a+b));
}
return function(b) {
if (typeof(b) !== "number") {
return undefined;
}
return args[0] + b;
}
}
//tests
console.log(addTogether(2,3));
console.log(addTogether(23, 30));
console.log(addTogether(5)(7));
console.log(addTogether("http://bit.ly/IqT6zt"));
console.log(addTogether(2, "3"));
console.log(addTogether(2)([3]));
</script>
</body>
</html>