# Consecutive Strings HELP

I’m unable to solve this codewars algorithm its very difficult this is the challenge : https://www.codewars.com/kata/consecutive-strings/train/javascript
I would appreciate it if someone can tell me where im going wrong
my code:

``````function longestConsec(strarr, k) {
console.log(strarr)
const n = strarr.length;
if (n === 0 || k > n || k <= 0) {
return "";
}

let longestString = "";

for (let word of strarr) {
if (word.length > longestString.length) {
longestString = word;
}
}
console.log(longestString)
let indexFirst = strarr.indexOf(longestString,1)
console.log(indexFirst)

if (!strarr[indexFirst+1])  {
let spliced = strarr.reverse().splice(strarr.indexOf(longestString),k)
console.log(spliced)

let reverseAgain = spliced.reverse().join("");
return reverseAgain;
}

// if (strarr[indexFirst+1].length < strarr[indexFirst].length && strarr[indexFirst+1].length < strarr[0].length) {

// for (let i = 0; i < strarr.length; i++) {
// if (strarr[indexFirst+1].length < strarr[indexFirst].length && strarr[indexFirst+1].length < strarr[i].length) {
//  let spliced = strarr.splice((indexFirst+1),1)
// console.log(spliced)
// }
// }

// }

let removed = strarr.splice(indexFirst,k)
console.log(removed)

return removed.join("");

}
longestConsec(["wlwsasphmxx","owiaxujylentrklctozmymu","wpgozvxxiu"], 2)

// 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 "".
``````

you are finding which is the longest string and working with that

instead, if these are the words length: `[6, 1, 9, 2, 6, 6]` and you have a `k` equal to `2` you need to return those two words with length 6+6 = 12, as that is the longest string you can do with two consecutive strings. Instead you are finding which is the longest word (the one of length 9) and doing things with that. But if you check, it will give you length 9+2=11

If you want to do my try, you can use function call `longestConsec(["666666", "1", "999999999", "22", "666666", "666666"], 2)`

And also if you do this: `longestConsec(["666666", "4444", "999999999", "22", "666666", "666666"], 2)`, this should return `"4444999999999"` with a length of 13, instead it returns `"99999999922"` with a length of 11

1 Like

at this point i’ll start from scratch since i misinterpreted the entire thing.

hey i think i solved it but im still UNABLE to pass the codewars for an unknown reason.
my re-done code:

``````function longestConsec(strarr, k) {
console.log(strarr)
const n = strarr.length;
if (n === 0 || k > n || k <= 0) {
return "";
}

let lengthChecker = "";

for ( let i = 0; i < strarr.length; i++) {
var str = "";
let sliced = strarr.slice(i,k)
console.log(sliced)
for (string of sliced) {
str+= string;
}
if (str.length > lengthChecker.length) {
lengthChecker = str;
}

k++
}

return lengthChecker;

}
// longestConsec(["666666", "1", "999999999", "22", "666666", "666666"], 2)
longestConsec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3)
``````

edit: nvm i removed the console logs and it passed. it was some kind of bug.

congratulation on passing

you can make it more efficient tho!
for example, if `k` is 3 and `n` is 10, do you ever need to have `i` as 8?

no because the final comparison is 7, 8 and 9?

exactly, so you can make the loop stop at a lower `i`, making it more efficient

i don’t think it makes any difference lol computers move at unimaginable speed

you may think so, but your algorithms can’t exceed a certain time of execution , or they will time out and not reach desired result and can’t pass the tests