# JavaScript Code NaN Problem

Hi there, my name is Alex, I’ve been using Glide for a few years now! I have always tried to refrain from posting questions because I don’t want to waste anyone’s time. But alas, I’m not a “real programmer” and I’ve hit a WALL! I’ve put maybe 8 hours into it already and I know its probably an easy fix but I just can’t figure it out, so I’m finally asking for help!

Here’s what I’m trying to accomplish: I have an array of numbers and I want to find the maximum consecutive numbers in the array. For example, an array of [2,3,4,6,8,10] would yield a result of 3, because 2,3,4, are consecutive numbers. I found a javaScript function online that claims to solve this but I cannot get it working. Again, I am NOT a programmer so I really don’t understand what it is saying or where potential problems might be.

I have attached pictures as well as the code I am currently using in the JavaScript column. I would GREATLY appreciate any input or advice anyone is willing to give. Thank you so much!

``````var arr = p1 ;
var n = arr.length;
function findLongestConseqSubseq(arr, n) {
let S = new Set();
for (let i = 0; i < n; i++)
let ans = 0;
for (let i = 0; i < n; i++) {

if (!S.has(arr[i] - 1)) {

let j = arr[i];

while (S.has(j))
j++;

ans = Math.max(ans, j - arr[i]);
}
}
return ans;
}
return findLongestConseqSubseq(arr, n);
``````

The problem is that you are passing a string to the JS column, and then trying to process it as an array.
So you need to do a couple of things:

• Firstly, remove the enclosing square brackets. You can do that with a simple template column.
• Secondly, adjust the JS code to coerce the string into an array of numbers. Just replacing the first line as follows will take care of that:
``````var arr = p1.split(',').map(Number);
``````

3 Likes

Darren, THANK YOU! You are a life-saver! That did the trick! Now all I have to do is patch the hole in the wall created by me banging me head against it trying to figure this out!!

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.