The challenge description is as follows:
You are given an array
strarr
of strings and an integerk
. Your task is to return the first longest string consisting ofk
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, ifn = 0
ork > n
ork <= 0
return""
.
I solved the challenge with as a side goal to follow and use ECMAScript 6 as much as possible. All suggestions on improving the code are welcome!
Note: I did not like using k
and n
in code, so I used numStr
and arrLen
respectively.
const longestConsec = (strArr, numStr) => { const arrLen = strArr.length if (arrLen === 0 || numStr > arrLen || numStr <= 0) { return "" } const consecStrings = getAllConsecutiveStrings(strArr, numStr, arrLen) return getFirstLongestString(consecStrings) } const getAllConsecutiveStrings = (strArr, numStr, arrLen) => { const numConsecStr = arrLen - numStr const result = [] let consecStr for (let i = 0; i <= numConsecStr; i++) { consecStr = "" for (let s = i; s < i + numStr; s++) { consecStr += strArr[s] } result.push(consecStr) } return result } const getFirstLongestString = strArr => { let firstlongestString = "" let longestLength = 0 for (let str of strArr) { strLen = str.length if (strLen > longestLength) { firstlongestString = str longestLength = strLen } } return firstlongestString }