-
Notifications
You must be signed in to change notification settings - Fork 0
/
myFunction.js
150 lines (117 loc) · 4.45 KB
/
myFunction.js
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
141
142
143
144
145
146
147
148
149
150
/*jshint esversion: 6 */
(function() {
"use strict";
function max(num1, num2) {
return num1>num2? num1:num2;
}
function maxOfThree(num1, num2, num3) {
const max1 =max(num1,num2);
return max1>num3? max1:num3;
}
function isVowel(char) {
const vowels = ['a','e','i','o','u'];
return vowels.includes(char)? true:false;
}
//*************************sum of array elements*************
function sumOfArray(array) {
let sum =0;
return array.reduce((num1,num2)=>{return num1+num2;},sum);
}
//*********** product of array elements *****************
function productOfArr(array) {
let product =1;
return array.reduce((num1,num2)=>{return num2*num1;}, product);
}
//********************* Reverse String ****************************
function reverse(str) {
let reveString = "";
for (let i = str.length - 1; i >= 0; i--) {
reveString += str[i];
}
return reveString;
}
//******************* Longest word ********************
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];
}
//******************* Longer than Filtered Word *****************************
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;
}
//
describe("MaxOfTwo", function() {
context("when 2 and 5 entered", function() {
it("max of the two is ", function() {
assert.equal(max(2,5), 5);
});
});
});
//******************** Max Of Three***************
describe("MaxOfThree", function() {
//*********** Max of Three Numbers ***************
context("when 2,8 and 5 entered", function() {
it("Maximum Number of the given three numbers is 8", function() {
assert.equal(maxOfThree(2,8,5), 8);
});
});
});
//******************* SUM OF ARRAYS *********************
describe("SumOfArray", function() {
//*********** Max of Three Numbers ***************
context("when [1,2,3,4] entered", function() {
it("Sum of the given Array is 10", function() {
assert.equal(sumOfArray([1,2,3,4]), 10);
});
});
});
//******************** PRODUCT OF ARRAYS *******************************
describe("ProductOfArray", function() {
//*********** Max of Three Numbers ***************
context("when [1,2,3,4] entered", function() {
it("Product of the given array 24 ", function() {
assert.equal(productOfArr([1,2,3,4]),24);
});
});
});
//******************** REVERSE A STRING **********************************
describe("reverseAstring", function() {
//*********** Max of Three Numbers ***************
context("when Nathanael is entered", function() {
it("Reverse is leanahtaN", function() {
assert.equal(reverse("Nathanael"),"leanahtaN");
});
});
});
//******************** LONGEST WORD **********************************
describe("longestWord", function() {
//*********** Max of Three Numbers ***************
context("when Banana, Mango, Apple, Guava,Pineapple is entered", function() {
it("The Longest is Pineapple ", function() {
assert.equal(longestWord(['Banana', 'Mango', 'Apple', 'Guava','Pineapple']),'Pineapple');
});
});
});
//******************** LONGEST WORD **********************************
describe("Words longer than 6 letters", function() {
//*********** Max of Three Numbers ***************
context("when Banana, Mango, Apple, Guava,Pineapple is entered", function() {
it("Word/s Longer than 6 letters is/are Pineapple ", function() {
assert.equal(filterLongWords(['Banana', 'Mango', 'Apple', 'Guava','Pineapple'],6),'Pineapple');
});
});
});
}());