-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab7-p2-jsintrohelper.html
140 lines (116 loc) · 4.49 KB
/
lab7-p2-jsintrohelper.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--
Look in the Chrome developer console to see the output. CTRL_SHIFT_J -->
<div >
<code id="output">
</code>
</div>
<script>
var outputDiv = document.getElementById("output");
/* runs test to see if expected argument is === to value returned by function2test argument */
function myFunctionTest(expected, function2test) {
if (expected+"" === function2test()+"") {
return "TEST SUCCEEDED";
} else {
return "TEST FAILED. Expected " + expected + " found " + function2test();
}
}
/* sum returns the sum of integer array */
function sum(arr) {
var sum=0;
// for(let i=0; i<arr.length;i++){
// sum+=arr[i];
// }
sum = arr.reduce((acc , num )=> acc+num);
return sum;
}
/* multiply returns the multiply of integer array */
function multiply(arr) {
var multiply=1;
for(let i=0; i<arr.length;i++){
multiply*=arr[i];
}
return multiply;
}
//reverse the string
function reverse(text){
let rev="";
for(let i=0;i<text.length;i++){
rev = text[i]+rev;
}
return rev;
}
//findLongestWord
function findLongestWord(wordArray){
let longLength=0;
wordArray.forEach(word => {
if(word.length>longLength){
longLength = word.length;
}
});
return longLength;
}
//findLongestWord
function filterLongWords(wordArray, i){
let newList=[];
newList= wordArray.filter(function(elem, index, array){
return elem.length > i;
});
return newList;
}
//Question 9
// const a = [1,3,5,3,3];
// const b = a.map(function(elem, i, array) {
// return elem * 10;
// })
// console.log(b);
// outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"[1,3,5,3,3] multiply each element by 10: "+b;
// const c = a.filter(function(elem, i, array){
// return elem === 3;});
// console.log(c);
// outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"[1,3,5,3,3] array with all elements equal to 3: "+c;
// const d = a.reduce(function(prevValue, elem, i, array){
// return prevValue * elem;
// });
// console.log(d);
// outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"[1,3,5,3,3] the product of all elements: "+d;
//TESTING
console.log("Expected output of sum([20,10]) is 30 " + myFunctionTest(30, function () {
return sum([20, 10]);
}));
outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"Expected output of sum([20,10]) is 30 " + myFunctionTest(30, function () {
return sum([20, 10]);
});
console.log("Expected output of multiply([20,10]) is 200 " + myFunctionTest(200, function () {
return multiply([20, 10]);
}));
outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"Expected output of multiply([20,10]) is 200 " + myFunctionTest(200, function () {
return multiply([20, 10]);
});
console.log("Expected output of reverse(jag testar) is ratset gaj " + myFunctionTest("ratset gaj", function () {
return reverse("jag testar");
}));
outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"Expected output of reverse(jag testar) is ratset gaj " + myFunctionTest("ratset gaj", function () {
return reverse("jag testar");
});
console.log("Expected output of findLongestWord(['Muhammad','Umar','Iqbal']) is 8 " + myFunctionTest(8, function () {
return findLongestWord(['Muhammad','Umar','Iqbal']);
}));
outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"Expected output of findLongestWord(['Muhammad','Umar','Iqbal']) is 8 " + myFunctionTest(8, function () {
return findLongestWord(['Muhammad','Umar','Iqbal']);
});
console.log("Expected output of filterLongWords(['Muhammad','Umar','Iqbal'],4) is ['Muhammad','Iqbal'] " + myFunctionTest(['Muhammad','Iqbal'], function () {
return filterLongWords(['Muhammad','Umar','Iqbal'],4);
}));
outputDiv.innerHTML = outputDiv.innerHTML+"<br>"+"Expected output of filterLongWords(['Muhammad','Umar','Iqbal'],4) is ['Muhammad','Iqbal'] " + myFunctionTest(['Muhammad','Iqbal'], function () {
return filterLongWords(['Muhammad','Umar','Iqbal'],4);
});
</script>
</body>
</html>