-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThirdGreatest.js
More file actions
112 lines (78 loc) · 2.89 KB
/
Copy pathThirdGreatest.js
File metadata and controls
112 lines (78 loc) · 2.89 KB
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
/*
Using the JavaScript language, have the function ThirdGreatest(strArr) take the array
of strings stored in strArr and return the third largest word within in. So for example:
if strArr is ["hello", "world", "before", "all"] your output should be world because
"before" is 6 letters long, and "hello" and "world" are both 5, but the output should
be world because it appeared as the last 5 letter word in the array. If strArr was
["hello", "world", "after", "all"] the output should be after because the first three
words are all 5 letters long, so return the last one. The array will have at least three
strings and each string will only contain letters.
Input = "coder","byte","code" Output = "code"
Input = "abc","defg","z","hijk" Output = "abc"
*/
function ThirdGreatest(strArr) {
var words = [];
var thirdGreatest, thirdGreatestStr, wordNow, wordArr;
var thirdGreatestArr = [];
for (var i = 0; i < strArr.length; i++) {
words.push([strArr[i].length, strArr[i]]);
}
words = words.sort(function(a,b){return a[0]<b[0]});
console.log(words);
for (var i = 0; i < words.length; i++) {
console.log("iterating");
wordNow = String(words[i]);
console.log(wordNow);
console.log(typeof wordNow);
wordArr = wordNow.split(',');
thirdGreatestArr.push(wordArr);
console.log(thirdGreatestArr);
console.log("words: "+words);
}
function SortDupes(thirdGreatestArr){
dupesArr = [];
for (j = 0; j < thirdGreatestArr.length; j++){
if(thirdGreatestArr[j][0] == thirdGreatestArr[2][0]){
dupesArr.push(thirdGreatestArr[j][1]);
}
}
console.log("dupesArr: " + dupesArr);
currentDupe = dupesArr[0];
console.log('currentDupe: ' + currentDupe);
console.log("strArr: " + strArr);
for (k = 0; k < dupesArr.length; k++){
if (strArr.indexOf(currentDupe) > strArr.indexOf(dupesArr[k])) {
console.log('currentDupexxx: ' + currentDupe);
} else {
currentDupe = dupesArr[k];
console.log('currentDupe: ' + currentDupe);
}
}
}
console.log("thirdGreatestArr: "+ thirdGreatestArr);
if(thirdGreatestArr[2][0] == thirdGreatestArr[1][0]){
SortDupes(thirdGreatestArr);
thirdGreatest = currentDupe;
return thirdGreatest;
} else if (thirdGreatestArr.length > 3){
if (thirdGreatestArr[2][0] == thirdGreatestArr[3][0]){
SortDupes(thirdGreatestArr);
} else {
thirdGreatest = thirdGreatestArr[2][1];
return thirdGreatest;
}
} else {
thirdGreatest = thirdGreatestArr[2][1];
return thirdGreatest;
}
}
ThirdGreatest(["byte","coder","code"]);
//ThirdGreatest(["abc","defg","z","hijk"]);
//ThirdGreatest(["coder","byte","code", "jerk","yeah","you"]);
//ThirdGreatest(["a","bc","def"]);
//ThirdGreatest(["two","three","four"]);
//ThirdGreatest(["a","bcd","efgdggc"]);
//ThirdGreatest(["aaaa","bbbbbb","cc"]);
//ThirdGreatest(["jk","lmn","opqrst"]);
//ThirdGreatest(["battt","catt","mat"]);
//ThirdGreatest(["as","assssvvvvt","affggg"]);