I'm trying to solve a code challenge on code wars and don't understand why I'm getting an error message regarding the .length of a string inside of an array when I try to run it.
It's definitely the first .length in line 7. I've tried running other arr[i].lengths and they work, I'm thinking it's a problem with scope?
function longestConsec(strarr, k) {
var arr = [];
if (strarr.length == 0 || k > strarr.length || k <= 0) {
return "";
}
for (var i = 1; i <= strarr.length; i++) {
if (strarr[i].length > strarr[0].length && arr.length < k) {
arr.push(strarr[i]);
}
}
arr.join('');
}
longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2);
Should return "abigailtheta" as it's the 2 longest strings after the
first string which were the requirements.
VM304:7 Uncaught TypeError: Cannot read property 'length' of undefined
console.log()or general debugging to find out where it's going wrong instead of assuming/guessing?strarrandkwhen the function is called?forisi = 6. Your array has no item with an index of6, sostrarr[6]isundefined.abigailtheta.