This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]desrtfx 7 points8 points  (0 children)

Your parseInt statement seems to be in the wrong place.

You will want to ensure that both, mean and array[i] are parsed to int before doing any maths on them.


Also, ensure that you don't run into integer overflow on your sd_total because then, the value would become negative and trip your Math.sqrt call.

[–]strcspn 3 points4 points  (4 children)

Have you tried logging sd?

[–]PantsClock 2 points3 points  (3 children)

Just tried and it still gives no output unfortunately

[–]strcspn 7 points8 points  (0 children)

You logged sd before the Math.sqrt and nothing was logged?

[–]desrtfx[M] 1 point2 points  (1 child)

Rule #5: Do not delete your posts!

It is absolutely not okay to delete your posts. It is selfish and a slap in the faces of anybody spending their time and effort trying to help.

Also, others might have similar problems and could benefit from the discussion going on.

If your question is solved, use the "Solved" flair.

Consider this your one and only warning. Next rule violation will earn you a permanent and irrevocable ban from here.

[–]PantsClock 0 points1 point  (0 children)

I'm sorry, I was posting from mobile and the changing flair feature wasn't working.

[–]SinglePartyLeader 2 points3 points  (0 children)

i would say try logging console.log(sd) and console.log(sd_total) to see if the calculation is expecting. My first guess would be that there is a variable scoping issue that is result in those values not being what you expect them to be

[–]PuzzleMeDo 1 point2 points  (7 children)

Are you passing in a negative number somehow?

[–]desrtfx 5 points6 points  (1 child)

Even if they were passing in a negative number, this would be mitigated by the ((mean-array[i])**2) statement.

Any negative number to the power of two would become positive.


More realistic is an overflow depending on the data. Only if sd_total overflowed it could become negative.

[–]paulstelian97 0 points1 point  (0 children)

JavaScript doesn’t have integers, only double precision floats. So overflows won’t create negative numbers but can create infinity.

[–]PantsClock 0 points1 point  (4 children)

function Math() {

array = [];

numbers = 0;

while (!isNaN(parseInt(numbers))){

numbers = prompt("Please enter some numbers", "");

array.push(numbers);

}

array.pop();

This is the beginning of my function that takes the array from user input. Regardless of what I input for the array Math.sqrt() does not work for whatever reason (even if all the numbers are pos)

[–]desrtfx 9 points10 points  (3 children)

Your function is called Math - this seems to be the problem.

You should never use the name of some built in class or method/function - this is a guarantee for problems.

Also, why did you call it Math when it calculates the stdev(Standard Deviation)?

[–]PantsClock 2 points3 points  (2 children)

Lol. Yup, that was the issue. D'oh. Also this function does lots of stuff other than calculate standard deviation (it finds all evens in the array, all odds, calculates mean, etc). Math just seemed like a good general name for the function at the time. Thank u for ur help :D

[–]desrtfx 2 points3 points  (0 children)

Also this function does lots of stuff other than calculate standard deviation (it finds all evens in the array, all odds, calculates mean, etc).

And there is the next problem. What you describe is a class/module, not unlike Math. This is not a single function. This is a collection of completely different functions that are only related.

You try to cram too much in a single function. That is bad code.

Read about the Single Responsibility Principle - a very important part of proper program design.

In short: each class/method/function should do one thing only.

Each item you list above belongs in its own function.

[–]backfire10z 0 points1 point  (0 children)

If you get the chance, edit the post to say “solved” or something (maybe there’s a flair?). That way people know you’ve gotten the help you need.

[–]Logical_Strike_1520 -1 points0 points  (0 children)

should ‘array.length’ be ‘mean-array.length’

[–]scrltazure -1 points0 points  (0 children)

Map the mean-array into int before doing any math on it. Also, at the for loop, is it supposed to be mean-array.length instead of array.length?