-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
84 lines (65 loc) · 2.6 KB
/
test.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html>
<head>
<title>Sample Function</title>
</head>
<body>
<h2>Instructions</h2>
<p>In javascript: Write a function that, given an array of integers (x), and an integer (y), returns true if any two of the integers in x add up to y. (Optional) Now, write a function that, given the above arguments and an additional integer (z), returns true if any z of the integers in x add up to y.</p>
<h2>Sample Function 1</h2>
<p>sampleFunction1(x, y), where x is array of ints and y is desired sum </p>
<h2>Sample Function 2</h2>
<p>sampleFunction2(x, y, z) where x is array of ints, y is desired sum, and z is number of operands</p>
</br>
<p>*both functions are accessible via the javascript console</p>
<script src="test.js"></script>
<script>
//would normally do this with jquery for cross browser support
document.addEventListener('DOMContentLoaded', function(){
var testCases = [
[ [1,2,3,4,5,6], 12, 3 ],
[ [2,2,2,2,2], 4, 3 ],
[ [1], 5, 2 ],
[ [1,1,1,1,1], 5, 5 ],
[ [10001, 10002, 1000, 2000, 500], 10000, 3 ],
[ [10001, 10002, 1000, 2000, 500], 13002, 3 ]
];
function runTest(arrOfIntVals, targetSum, numberOfOperands){
console.log('Can [' + arrOfIntVals + '] add up to ' + targetSum + ' using ' + numberOfOperands + ' value' + (numberOfOperands === 1? '' : 's') + '? '
+ (sampleNamespace.canArraySumTo(arrOfIntVals,targetSum, numberOfOperands) ? 'Yes' : 'No' ) );
}
for(var i=0; i<testCases.length; i++){
runTest.apply(this, testCases[i]);
}
});
//x expected to be array of integers
//y expected to be target sum
function sampleFunction1(x, y){
//simple checks for valid arguments
if(!(x instanceof Array)){
throw "first argument must be an array";
}
if(isNaN(y)){
throw "second argument must be a number";
}
return sampleNamespace.canArraySumTo(x, y, 2);
}
//x expected to be array of integers
//y expected to be target sum
//z expected to be number of operands
function sampleFunction2(x, y, z){
//simple checks for valid arguments
if(!(x instanceof Array)){
throw "first argument must be an array";
}
if(isNaN(y)){
throw "second argument must be a number";
}
if(isNaN(z)){
throw "third argument must be a number";
}
return sampleNamespace.canArraySumTo(x, y, z);
}
</script>
</body>
</html>