Repository navigation
Expand file tree
/
Copy pathexamplelib-conforms.js
More file actions
65 lines (56 loc) · 1.91 KB
/
Copy pathexamplelib-conforms.js
File metadata and controls
65 lines (56 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
/**
* Simple example of a library that conforms to ArrayLike.js array checking
* to check that an object has Array methods instead of requiring supplied
* arguments to be genuine JavaScript Arrays.
*/
(function(window, document, undefined) {
var toString = Object.prototype.toString,
examplelib = window.examplelib || (window.examplelib = {});
// main isArrayLike method
function isArrayLike(obj) {
return obj && obj.__ArrayLike || Object.prototype.toString.call(obj) === '[object Array]';
}
// this is just for testing - usually a library would only have the one above
function isArrayLikeSecondary(obj) {
return obj && obj.__ArrayLike || Array.isArray(obj);
}
// given an array, show the result of addition of elements
function displaySum(arr) {
if(!isArrayLike(arr)) {
alert("displaySum: Sorry " + arr + " is not ArrayLike");
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(!isArrayLikeSecondary(arr)) {
alert("displayProduct: Sorry " + arr + " is not ArrayLike");
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(!Array.isArrayLike(arr)) {
alert("displayReversed: Sorry " + arr + " failed Array.isArrayLike");
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);