# JavaScript Code NaN Problem

**URL:** <https://community.glideapps.com/t/javascript-code-nan-problem/38833>\
**Category:** Ask for Help\
**Created:** [February 22, 2022, 2:39pm UTC](https://community.glideapps.com/t/javascript-code-nan-problem/38833 "2022-02-22T14:39:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Wuerth](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/alex_wuerth/32/180_2.png) [@Alex\_Wuerth](https://community.glideapps.com/u/Alex_Wuerth)\
**Post date:** [February 22, 2022, 2:39pm UTC](https://community.glideapps.com/t/javascript-code-nan-problem/38833/1 "2022-02-22T14:39:01Z")

</div>

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!

```auto
var arr = p1 ;
var n = arr.length;
function findLongestConseqSubseq(arr, n) {
   let S = new Set();
    for (let i = 0; i < n; i++)
        S.add(arr[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);

```

 ![Screenshot 2022-02-22 052613](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/6/a/6a5f4188a2cef77e053ebf863c5d6a146afd4509.png)  
 ![Screenshot 2022-02-22 052510](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/0/308c37fcea6b95a2853efabc999b4e8584a02aa9.png)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [February 22, 2022, 3:22pm UTC](https://community.glideapps.com/t/javascript-code-nan-problem/38833/2 "2022-02-22T15:22:48Z")

</div>

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:

```auto
var arr = p1.split(',').map(Number);

```

 ![Screen Shot 2022-02-22 at 11.21.45 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/4/5/4526c3e1082030bda6c20d6a26f5f472c1552d99.png)

---

<div class="post-metadata">

**Author:** ![Alex\_Wuerth](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/alex_wuerth/32/180_2.png) [@Alex\_Wuerth](https://community.glideapps.com/u/Alex_Wuerth)\
**Post date:** [February 22, 2022, 3:26pm UTC](https://community.glideapps.com/t/javascript-code-nan-problem/38833/3 "2022-02-22T15:26:10Z")

</div>

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!! 🤣

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [February 23, 2022, 3:27pm UTC](https://community.glideapps.com/t/javascript-code-nan-problem/38833/4 "2022-02-23T15:27:08Z")

</div>

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