-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiplyStrings.js
More file actions
46 lines (42 loc) · 1.26 KB
/
multiplyStrings.js
File metadata and controls
46 lines (42 loc) · 1.26 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
// given an array of strings, find the max value of a pair only if the pair has
// no letters in common it's a little wordy. Prob could make this better
function multiplyStrings(array) {
let max = 0;
for (let i = 0; i < array.length - 1; i++) { //map over array
var index = array[i]; //index of the array
for (let k = 0; k < array.length; k++) { //map over the array a second time
if (k !== i) { //we don't want to compare the index to itself
var nextItem = array[k];
if ((index.length * nextItem.length) > max && !anythingInCommon(index, nextItem)) {
max = index.length * nextItem.length;
}
}
}
}
return max;
}
function anythingInCommon(stra, strb) { //function for comparing substrings in a string
for (var i = 0, len = stra.length; i < len; i++) {
if (strb.indexOf(stra[i]) != -1)
return true;
}
return false //nothing in common
}
console.log(multiplyStrings([
"abcw",
"baz",
"foo",
"bar",
"xtfn",
"abcdef"
])); //16
console.log(multiplyStrings([
"a",
"ab",
"abc",
"d",
"cd",
"bcd",
"abcd"
])); //4
console.log(multiplyStrings(["a", "aa", "aaa", "aaaa"])); //0