-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonSuffix.js
45 lines (43 loc) · 1003 Bytes
/
commonSuffix.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
function commonSuffix(arr){
var first;
var second;
var pending;
var suffix;
for(var j = 1; j < arr.length; j++){
pending = '';
for(var i = 1; i < arr[j].length + 1; i++){
first = arr[0][arr[0].length - i];
second = arr[j][arr[j].length - i];
if(first == second){
pending += first;
} else {
break;
}
}
if(!suffix || pending.length < suffix.length){
suffix = pending;
}
}
return reverse(suffix);
}
function reverse(str){
if(str === undefined){
return '';
}
var length = str.length;
var new_str = '';
for(var i = length - 1; i > -1; i--){
new_str += str[i];
}
return new_str;
}
var result = commonSuffix(["carnation", "persuasion", "satisfaction", "megaton"]);
console.log(result);
var result2 = commonSuffix(["basketball", "potato", "glasses"]);
console.log(result2);
var result3 = commonSuffix([]);
console.log(result3);
var result4 = commonSuffix(["same", "same"]);
console.log(result4);
var result5 = commonSuffix(["cody"]);
console.log(result5)