-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkata5.js
38 lines (36 loc) · 1.85 KB
/
kata5.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
/**
You are given an array strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array.
Example:
longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta"
n being the length of the string array, if n = 0 or k > n or k <= 0 return "".
function testing(actual, expected) {
Test.assertEquals(actual, expected)
}
Test.describe("longestConsec",function() {
Test.it("Basic tests",function() {
testing(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2), "abigailtheta")
testing(longestConsec(["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"], 1), "oocccffuucccjjjkkkjyyyeehh")
testing(longestConsec([], 3), "")
testing(longestConsec(["itvayloxrp","wkppqsztdkmvcuwvereiupccauycnjutlv","vweqilsfytihvrzlaodfixoyxvyuyvgpck"], 2), "wkppqsztdkmvcuwvereiupccauycnjutlvvweqilsfytihvrzlaodfixoyxvyuyvgpck")
testing(longestConsec(["wlwsasphmxx","owiaxujylentrklctozmymu","wpgozvxxiu"], 2), "wlwsasphmxxowiaxujylentrklctozmymu")
testing(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], -2), "")
testing(longestConsec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3), "ixoyx3452zzzzzzzzzzzz")
testing(longestConsec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 15), "")
testing(longestConsec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 0), "")
})})
*/
// first try
function longestConsec(strarr, k) {
// your code
var length = strarr.length;
var result = '';
var slice;
if ( k <= 0 || k > length || length === 0) return '';
for (var i = 0, j = i + ( k - 1); j < length; i++ , j++) {
slice = strarr.slice(i, j + 1).join('');
if ( slice.length > result.length ) {
result = slice;
}
}
return result;
}