0

I have a program that reads a specific text file from a coding challenge that I've recieved and it takes the numbers and puts it into an array for me to solve a quadratic equation. When I go to display my answers I keep getting the NaN error on all of my values and I cant find where I messed up.

CODE

var lines = data[0].split("/n");
var numQuads = lines[0];
for (var i = 1; i < numQuads; i++){
  var fields = lines[i].split(",");
  var a = fields[0];
  var b = fields[1];
  var c = fields[2];
}
a = parseInt();
b = parseInt();
c = parseInt();
var discr = (b * b) - (4 * (a * c));
var sqrDiscr = Math.sqrt(discr);
var x = (-b + sqrDiscr) / (2*a);
var y = (-b - sqrDiscr) / (2*a);
var outputL = "The quadratic equation with coefficients A = " + a + " B = " + b + " C= " + c + " has no real roots!";
var outputW = "The quadratic equation with coefficients A = " + a + " B = " + b + " C= " + c + " has roots x = " + x + " and x = " + y;
if (discr >= 0) {
  output += outputW + "\n";
}
else {
  output += outputL + "\n\n";
}
  • 3
    You have `a = parseInt();` - you need `a = parseFloat(a);` –  Feb 16 '17 at 15:42
  • 2
    `parseInt` is a function that takes a string as a parameter and returns an integer. When you call it as `parseInt()` with no arguments, it’s not doing anything useful. – Ry- Feb 16 '17 at 15:42
  • You didn't declared the array `data`. – user7393973 Feb 16 '17 at 15:42
  • I declared the data array further up in my code but even with the parseFloat im still getting the NaN error – Mason Garrett Feb 16 '17 at 15:45
  • @MasonGarrett, then you didn't declared the variable `output`. – user7393973 Feb 16 '17 at 15:46
  • You should always provide the minimal code so we can check it and see where the errors are. – user7393973 Feb 16 '17 at 15:46
  • My teacher has it set up so i use this template to display my answers so I have the output variable already setup as well as the data array setup above. The array was working to simply display the text file in the output but when I started to add the loop it gave me the NaN – Mason Garrett Feb 16 '17 at 15:50

5 Answers5

0

You did not provide an argument to the parseInt function. It works like this: parseInt("2") for example. You probably want to use parseFloat instead of parseInt.

Another remark: your data array is undefined.

PinkFluffyUnicorn
  • 1,260
  • 11
  • 20
0

you have insert String in parseInt()

a = parseInt("67");
b = parseInt("3");
c = parseInt("2");
0

Should probably be:

a = parseInt(a);
b = parseInt(b);
c = parseInt(c);
Jonas Eriksson
  • 171
  • 2
  • 12
0

The problem is that you are not parsing anything with your parse int. Take a look here for some docs on parseInt. Anyway that's how it should look like in your code:

a = parseInt(a, 10);
b = parseInt(b, 10);
c = parseInt(c, 10);
d = parseInt(d, 10);

EDIT: following the suggestion of @d3l I looked into the parseInt parameters, according to this question there could be some unexpected behaviours of the parseInt function without adding the radix parameter. Hence I added it to my solution.

Assuming you are parsing integers we can specify 10 as base.

Community
  • 1
  • 1
rakwaht
  • 3,666
  • 3
  • 28
  • 45
0

the problem was var lines = data[0].split("/n"); I used the wrong character. It was supposed to be var lines = data[0].split("\n");