Repository navigation
Expand file tree
/
Copy pathexamplelib-compatible.js
More file actions
66 lines (57 loc) · 1.91 KB
/
Copy pathexamplelib-compatible.js
File metadata and controls
66 lines (57 loc) · 1.91 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Simple example of a library that uses both best practices for determining
* if an object is a JavaScript Array: Object.prototype.toString and
* Array.isArray. Though this library does not use ArrayLike.js conventions,
* both of these checks can be coerced with the ArrayLikeIsArray.
*/
(function(window, document, undefined) {
var toString = Object.prototype.toString,
examplelib = window.examplelib || (window.examplelib = {});
// main isArray method
function isArray(obj) {
return (Object.prototype.toString.call(obj) === '[object Array]');
}
// this is just for testing - usually a library would only have the one above
function isArraySecondary(obj) {
return (Array.isArray(obj));
}
// given an array, show the result of addition of elements
function displaySum(arr) {
if(!isArray(arr)) {
alert("displaySum: Sorry " + arr + " is not an array");
return;
}
var sum = 0;
for(var i=0; i<arr.length; i++) {
sum += arr[i];
}
document.body.innerHTML += arr.join(" + ") + " = " + sum + "<p>";
}
// given an array, show the result of multiplication of elements
function displayProduct(arr) {
if(!isArraySecondary(arr)) {
alert("displayProduct: Sorry " + arr + " is not an array");
return;
}
var product = 1;
arr.forEach(function (element) {
product *= element;
});
document.body.innerHTML += arr.join(" * ") + " = " + product + "<p>";
}
// given an array, show the result of multiplication of elements
function displayReversed(arr) {
if(!isArray(arr)) {
alert("displayReversed: Sorry " + arr + " is not an array");
return;
}
document.body.innerHTML += "reverse: " + arr + " => ";
arr.reverse();
document.body.innerHTML += arr + "<p>";
arr.reverse(); // put it back...
}
// publish external api
examplelib.displaySum = displaySum;
examplelib.displayProduct = displayProduct;
examplelib.displayReversed = displayReversed;
})(window, document);