Skip to content
← Back to Community
How come it shows up as NaN even though it is a number?
Profile icon
ComputingSquid

I'm confused. So this was a project for my Dad to calculate mean deviation. But then i was like yay it should work and the JavaScript looked ok, but what was wrong with innerHTML?
because it said NaN instead of the actual mean deviation.
please help.

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
rflctnsmusic
Profile icon
ComputingSquid
Comments
hotnewtop
Profile icon
Coder100

ok so basically the error is that all the meandev variables are not defined:

let meanDev1; let meanDev2; let meanDev3; let meanDev4; let meanDev5;

This is because in your function:

function calc(theVar, meanDev) {

You don't own meanDev (meaning you can't change the variable that gave you that value). The solution is to just return the value.

Solution

ALSO ADD SEMICOLONS THIS IS NOT PYTHON

Profile icon
ComputingSquid

ok fine @Coder100

Profile icon
Coder100

so did your problem get solved @ComputingSquid

Profile icon
ComputingSquid

@Coder100
image
nope

Profile icon
ComputingSquid

😢 sad @Coder100

Profile icon
Coder100

what was your input @ComputingSquid

Profile icon
Coder100

um you didn't even use my code @ComputingSquid

Profile icon
ComputingSquid
Profile icon
Coder100

lol your code has to be

function calc(theVar) { if (theVar > mean) { return theVar - mean; } else if(x1 < mean) { return mean - theVar; } else {//x=mean return 0; } } meanDev1 = calc(x1); meanDev2 = calc(x2); meanDev3 = calc(x3); meanDev4 = calc(x4); meanDev5 = calc(x5);

didn't I tell you lol
@ComputingSquid

Profile icon
ComputingSquid

ok fixed i think @Coder100

Profile icon
Coder100
Profile icon
ComputingSquid

not again it still is not fixed @Coder100

Profile icon
Coder100

why are you still not using my code @ComputingSquid

Profile icon
Coder100

why are you not setting the variable

You have

calc(x1); calc(x2); calc(x3); calc(x4); calc(x5);

I have

meanDev1 = calc(x1); meanDev2 = calc(x2); meanDev3 = calc(x3); meanDev4 = calc(x4); meanDev5 = calc(x5);

@ComputingSquid

Profile icon
ComputingSquid
Profile icon
Coder100

no you are not look at what I have and look at what you have you have to set the variable to that value @ComputingSquid

Profile icon
Coder100

my function only returns the result you have to set that result to the variable smh @ComputingSquid

Profile icon
ComputingSquid

yay ok it works i think @Coder100

Profile icon
Coder100
Profile icon
ComputingSquid

we commented so much we at top of feed lol
YayTopFeed
@Coder100

Profile icon
Coder100
Profile icon
ComputingSquid
Profile icon
Coder100
Profile icon
Coder100

who knew comments had that much impact xD @ComputingSquid

Profile icon
ComputingSquid

me: 🤣🤣🤣🤣🤣🤣🤣 @Coder100

Profile icon
Coder100
Profile icon
ComputingSquid

i dunno maybe i should advertise my repls on my post haha @Coder100

Profile icon
Coder100
Profile icon
ComputingSquid
Profile icon
Coder100

image

Can I ask why you are doing Number(meanDev) and then discarding the result?

Profile icon
Coder100

where?

Profile icon
ComputingSquid

wait what @Coder100

Profile icon
Coder100

ok read my solution
this is a very big mistake never ever do this again @ComputingSquid

Profile icon
ComputingSquid

thank you @Coder100

Profile icon
Coder100

no problem :) mark as correct answer? @ComputingSquid

Profile icon
ComputingSquid

i did your welcome @Coder100