I have a strange error when using Array.map() method. Why does arrayElementLength=strarr[index+1].length throws an error that its undefined (even though it works)
function longestConsec(strarr, k) { // your code let solution = []; strarr.map((string, index, arr) => { let arrayElementLength = strarr[index+1].length; console.log(arrayElementLength); if(string.length == arrayElementLength){ solution.push(string); } }); return solution; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
When you reach to the last index of strArr
than with index+1
you're trying to access index which is not present in array, and this is cause of the this error strarr[(index + 1)] is undefined
function longestConsec(strarr, k) { // your code let solution=[]; strarr.map((string,index,arr)=>{ let arrayElementLength=strarr[index+1].length; console.log(arrayElementLength); if(string.length==arrayElementLength){ solution.push(string); } }) return solution; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
On side note: you're not using return value of map so it's better to use forEach
Why does arrayElementLength=strarr[index+1].length throws an error that its undefined (even though it works)
No, it does not work in the last iteration. The array index starts from 0
. With strarr[index+1]
, in the last iteration, your code tries to access the item from an index which really does not exist and returns undefined
.
var sampleArr = ['test']; console.log(sampleArr[0]); //test console.log(sampleArr[1]); //undefined
So, remove +1
from the index part and your code will work as expected:
function longestConsec(strarr, k) { // your code let solution=[]; strarr.map((string,index,arr)=>{ let arrayElementLength=strarr[index].length; console.log(arrayElementLength); if(string.length==arrayElementLength){ solution.push(string); } }) return solution; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
It's not strange, the array index starts at 0 so the last index + 1 will be undefined.
Furthermore, Array.map
is usually used to "map" the given array to a new array, the way you are using it would be more appropriate to use Array.forEach
.
function longestConsec(strarr, k) {
let solution = [];
strarr.forEach((string, index, arr) => {
let arrayElementLength = strarr[index].length;
console.log(arrayElementLength);
if(string.length === arrayElementLength){
solution.push(string);
}
})
return solution;
}
console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
As others have already mentioned, you're getting the error when the loop reaches the last element. strarr[index+1]
returns undefined
and you can't access it's length
.
Looking at your code, if your intention is to get all the items which have the same length
as the next element, filter
suits your purpose much better:
function longestConsec(strarr, k) { return strarr.filter((string, index) => { return index !== strarr.length - 1 && string.length === strarr[index + 1].length }) } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"], 2))
I have added an additional check index !== strarr.length - 1
if the loop has encountered the last element.
Array.map
iterates over every element in the map and the index
(second argument) is the current index, now if you reach the last element in the array you're mapping, the next element ( index + 1
) simply does not exist.
If the function as a whole is trying to obtain the longest streak of same length stings, you might just want to reduce
the base array.
function longestConsec(arr) { return arr .reduce((carry, string) => { const prev = carry[carry.length - 1]; const length = string.length; if (!prev || prev.length !== string.length) { carry.push({ length, strings: [string] }); } else { prev.strings.push(string); } return carry; }, []) .sort((one, two) => two.strings.length - one.strings.length) .shift().strings; } console.log(longestConsec(["zone", "abigail", "theta", "form", "libe", "zas"]))
(Note that I removed the k
argument from the function, as it didn't seem to be used)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.