Skip to content

Commit 7e9352b

Browse files
Merge pull request uagc-it-readiness#5 from robinsonaaron/master
Added JavaScript assignment 3
2 parents b69f152 + 6fd1b0f commit 7e9352b

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

02-javascript/assignment-3/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The sum of a range
2+
3+
The introduction of this book alluded to the following as a nice way to compute the sum of a range of numbers:
4+
5+
console.log(sum(range(1, 10)));
6+
Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end.
7+
8+
Next, write a sum function that takes an array of numbers and returns the sum of these numbers. Run the previous program and see whether it does indeed return 55.
9+
10+
As a bonus assignment, modify your range function to take an optional third argument that indicates the “step” value used to build up the array. If no step is given, the array elements go up by increments of one, corresponding to the old behavior. The function call range(1, 10, 2) should return [1, 3, 5, 7, 9]. Make sure it also works with negative step values so that range(5, 2, -1) produces [5, 4, 3, 2].
11+
12+
```
13+
console.log(range(1, 10));
14+
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
15+
console.log(range(5, 2, -1));
16+
// → [5, 4, 3, 2]
17+
console.log(sum(range(1, 10)));
18+
// → 55
19+
```
20+
21+
source: [Eloquent JavaScript](http://eloquentjavascript.net/04_data.html)

02-javascript/assignment-3/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<script type="text/javascript" src="main.js" ></script>
4+
</head>
5+
<body>
6+
<input type="button" onclick="testRangeFunction()" value="Test range function" />
7+
<input type="button" onclick="testSumFunction()" value="Test sum array function" />
8+
</body>
9+
</html>

02-javascript/assignment-3/main.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
function range(start, end) {
2+
// your code here
3+
}
4+
5+
function sum(array) {
6+
// your sum function here
7+
}
8+
9+
10+
// do not edit below this line
11+
12+
// test the range function
13+
function testRangeFunction() {
14+
var resultString = "testRangeFunction:";
15+
16+
if ([1,2,3,4,5,6,7,8,9,10].equals(range(1, 10))
17+
&& [0,1,2,3,4,5].equals(range(0, 5)))
18+
resultString += "pass";
19+
else
20+
resultString += "fail";
21+
22+
console.log(resultString);
23+
}
24+
25+
// test the range function
26+
function testSumFunction() {
27+
var resultString = "testSumFunction:";
28+
29+
if (sum(range(1,10) === 55))
30+
resultString += "pass";
31+
else
32+
resultString += "fail";
33+
34+
console.log(resultString);
35+
}
36+
37+
38+
// Warn if overriding existing method
39+
if(Array.prototype.equals)
40+
console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
41+
// attach the .equals method to Array's prototype to call it on any array
42+
Array.prototype.equals = function (array) {
43+
// if the other array is a falsy value, return
44+
if (!array)
45+
return false;
46+
47+
// compare lengths - can save a lot of time
48+
if (this.length != array.length)
49+
return false;
50+
51+
for (var i = 0, l=this.length; i < l; i++) {
52+
// Check if we have nested arrays
53+
if (this[i] instanceof Array && array[i] instanceof Array) {
54+
// recurse into the nested arrays
55+
if (!this[i].equals(array[i]))
56+
return false;
57+
}
58+
else if (this[i] != array[i]) {
59+
// Warning - two different object instances will never be equal: {x:20} != {x:20}
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
// Hide method from for-in loops
66+
Object.defineProperty(Array.prototype, "equals", {enumerable: false});

0 commit comments

Comments
 (0)