-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicJavaScript.html
132 lines (117 loc) · 5.81 KB
/
basicJavaScript.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Java Scipt</title>
<script>
function myFunctionTest(expected, found) {
if (expected === found) {
return "TEST SUCCEEDED";
} else {
return "TEST FAILED. Expected " + expected + " found " + found;
}
}
function max(num1, num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
function maxOfThree(num1, num2, num3) {
let big = 0;
big = max(num1, num2);
return (max(big, num3));
}
function isVowel(char) {
if (char === "a" || char === "e" || char === "i" || char === "o" || char === "u") {
return true;
}
return false;
}
function sumOfArray(arrOfInt) {
let total = 0;
for (let i = 0; i < arrOfInt.length; i++) {
if (isNaN(arrOfInt[i])) {
continue;
}
total += arrOfInt[i];
}
return total;
}
function productOfArray(arrOfInt) {
let product = 1;
for (let i = 0; i < arrOfInt.length; i++) {
if (isNaN(arrOfInt[i])) {
continue;
}
product *= arrOfInt[i];
}
return product;
}
function reverse(str) {
let reveString = "";
for (let i = str.length - 1; i >= 0; i--) {
reveString += str[i];
}
return reveString;
}
function longestWord(listOfWord) {
let indexOfLongest = -1;
let sizeOfMax = -1;
for (let i = 0; i < listOfWord.length; i++) {
if (sizeOfMax < listOfWord[i].length) {
indexOfLongest = i;
sizeOfMax = listOfWord[i].length;
}
}
return listOfWord[indexOfLongest];
}
function filterLongWords(arrOfWords, len) {
let filtered = [];
for (let i = 0; i < arrOfWords.length; i++) {
if (arrOfWords[i].length > len) {
filtered.push(arrOfWords[i]);
}
}
return filtered;
}
function display() {
let span = document.getElementById("output1");
let span1 = document.getElementById("output2");
let span2 = document.getElementById("output3");
let span3 = document.getElementById("output4");
let span4 = document.getElementById("output5");
let span5= document.getElementById("output6");
let span6 = document.getElementById("output7");
span.innerHTML = "Q1:" + "MAXIMUM OF TWO NUMBERS:: The Expected Output output of max(23,8) is 23," + myFunctionTest(23, max(23, 8));
span1.innerHTML = "Q2:" + "MAXIMUM OF THREE NUMBERS:: The Expected Output output of maxOfThree(23,8,45) is 45," + myFunctionTest(45, maxOfThree(23, 8, 45));
span2.innerHTML = "Q3:" + "SUM OF ARRAYS:: The Expected Output output of sumOfArray([23,8,45,-20,5]) is 61," + myFunctionTest(61, sumOfArray([23, 8, 45, -20, 5]));
span3.innerHTML = "Q4:" + "PRODUCT OF ARRAY ELEMENTS:: The Expected Output output of productOfArray([2, 8, 4,3, 5]) is 960," + myFunctionTest(960, productOfArray([2, 8, 4, 3, 5]));
span4.innerHTML = "Q5:" + "REVERSE STRING:: The Expected Output output of reverse('JavaScript') is 'tpircSavaJ'," + myFunctionTest('tpircSavaJ', reverse('JavaScript'));
span5.innerHTML = "Q6:" + "LONGEST WORD:: The Expected Output output of longestWord([Banana, Pinneaple,Dates,Mango]) is Pineapple," + myFunctionTest("Pineapple", longestWord(["Banana", "Pineapple", "Dates", "Mango"]));
span6.innerHTML = "Q7:" + "FILTER ARRAY:: The Expected Output output of filterLongWords([Banana, Pinneaple,Dates,Mango],5) is Banana, Pinneaple," + myFunctionTest("Pineapple", filterLongWords(["Banana", "Pineapple", "Dates", "Mango"], 6)); }
console.log("MAXIMUM OF TWO NUMBERS:: The Expected Output output of max(23,8) is 23," + myFunctionTest(23, max(23, 8)));
console.log("MAXIMUM OF THREE NUMBERS:: The Expected Output output of maxOfThree(23,8,45) is 45," + myFunctionTest(45, maxOfThree(23, 8, 45)));
console.log("SUM OF ARRAYS:: The Expected Output output of sumOfArray([23,8,45,-20,5]) is 61," + myFunctionTest(61, sumOfArray([23, 8, 45, -20, 5])));
console.log("PRODUCT OF ARRAY ELEMENTS:: The Expected Output output of productOfArray([2, 8, 4,3, 5]) is 960," + myFunctionTest(960, productOfArray([2, 8, 4, 3, 5])));
console.log("REVERSE STRING:: The Expected Output output of reverse('JavaScript') is 'tpircSavaJ'," + myFunctionTest('tpircSavaJ', reverse('JavaScript')));
console.log("LONGEST WORD:: The Expected Output output of longestWord([Banana, Pinneaple,Dates,Mango]) is Pineapple," + myFunctionTest("Pineapple", longestWord(["Banana", "Pineapple", "Dates", "Mango"])));
console.log("FILTER ARRAY:: The Expected Output output of filterLongWords([Banana, Pinneaple,Dates,Mango],6) is Pineapple," + myFunctionTest(["Banana","Pineapple"], filterLongWords(["Banana", "Pineapple", "Dates", "Mango"], 5)));
</script>
</head>
<body>
<body>
<h1>Assignment on JavaScript</h1>
<button onclick="display();">Click This Button to see all the answers</button><br/>
<br/>
<span id="output1">xxxxxxxx</span><br/>
<span id="output2">xxxxxxxx</span><br/>
<span id="output3">xxxxxxxx</span><br/>
<span id="output4">xxxxxxxx</span><br/>
<span id="output5">xxxxxxxx</span><br/>
<span id="output6">xxxxxxxx</span><br/>
<span id="output7">xxxxxxxx</span><br/>
</body>
</body>
</html>