# Unable to convert NaN to a number - found segment of code responsible but still can't debug

**URL:** <https://discourse.psychopy.org/t/unable-to-convert-nan-to-a-number-found-segment-of-code-responsible-but-still-cant-debug/33171>\
**Category:** Online experiments\
**Created:** [February 7, 2023, 6:26pm UTC](https://discourse.psychopy.org/t/unable-to-convert-nan-to-a-number-found-segment-of-code-responsible-but-still-cant-debug/33171 "2023-02-07T18:26:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexisaylow](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alexisaylow/32/15731_2.png) [@alexisaylow](https://discourse.psychopy.org/u/alexisaylow)\
**Post date:** [February 7, 2023, 6:26pm UTC](https://discourse.psychopy.org/t/unable-to-convert-nan-to-a-number-found-segment-of-code-responsible-but-still-cant-debug/33171/1 "2023-02-07T18:26:07Z")

</div>

Exact error:

- when converting an object to its numerical form
- **unable to convert NaN to a number**

I’ve found the segment of code responsible for this, but I’m not sure how to debug it:

var pointA = minForCertainty.getRating();  
var pointB = maxForCertainty.getRating();

if ((pointA !== null) && (Number.isNaN(pointA) == false)){  
if ((pointB !== null) && (Number.isNaN(pointB) == false)) {  
linewidth = pointB - pointA;  
lineposition = ((pointA + pointB) / 2);  
}  
}

thisExp=psychoJS.experiment  
if (((endkey.keys !== undefined) && (endkey.keys === “space”))) {  
thisExp.addData(‘linewidth’, linewidth)  
thisExp.addData(‘lineposition’, lineposition)  
}

(in ‘Every Frame’ - initial values of linewidth, lineposition, pointA and pointB have been set in begin experiment & before experiment)

I’ve tried looking through the forum and checking the python to JS cribsheet, but nothing seems to come up. The python version of this code works offline:

pointA = minForCertainty.getRating()  
pointB = maxForCertainty.getRating()

if pointB is not None:  
if pointA is not None:  
linewidth = pointB - pointA  
lineposition = (pointA + pointB) / 2

if ‘space’ in endkey.keys:  
thisExp.addData(‘linewidth’, linewidth)  
thisExp.addData(‘lineposition’, lineposition)

Thank you!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 7, 2023, 7:20pm UTC](https://discourse.psychopy.org/t/unable-to-convert-nan-to-a-number-found-segment-of-code-responsible-but-still-cant-debug/33171/2 "2023-02-07T19:20:59Z")

</div>

Does this help?

> [@Unable to convert NaN to a number](https://discourse.psychopy.org/t/unable-to-convert-nan-to-a-number/18713/2):
>
> Hi @wakecarter, looks like the input to setPos() is taken through toNumerical() for sanity checking. That the latter is throwing means it is working as expected I believe. I would need to look closer to determine why ypos comes up NaN in the first place, but for now you should be able to use [Number.isNaN()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) instead of the auto-translated ypos === NaN to check if ypos is usable or not, x

---

<div class="post-metadata">

**Author:** ![alexisaylow](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alexisaylow/32/15731_2.png) [@alexisaylow](https://discourse.psychopy.org/u/alexisaylow)\
**Post date:** [February 7, 2023, 8:30pm UTC](https://discourse.psychopy.org/t/unable-to-convert-nan-to-a-number-found-segment-of-code-responsible-but-still-cant-debug/33171/3 "2023-02-07T20:30:31Z")

</div>

I did look at that thread, sadly it didn’t help ☹ Followed the advice to use Number.isNaN but it didn’t seem to make a difference

---

<div class="post-metadata">

**Author:** ![alexisaylow](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alexisaylow/32/15731_2.png) [@alexisaylow](https://discourse.psychopy.org/u/alexisaylow)\
**Post date:** [February 9, 2023, 6:35pm UTC](https://discourse.psychopy.org/t/unable-to-convert-nan-to-a-number-found-segment-of-code-responsible-but-still-cant-debug/33171/4 "2023-02-09T18:35:05Z")

</div>

So I found a solution from [Demonstrate the slider value - Online experiments - PsychoPy](https://discourse.psychopy.org/t/demonstrate-the-slider-value/22767/8) - slider.getRating doesn’t work on JS, which I did not know!! Use slider.\_markerPos instead (see Becca’s comment on linked post).

@wakecarter possible addition to the cribsheet?
