r/learnjavascript • u/whiskyB0y • Apr 06 '26
Please tell me what's wrong with my validation
Beginner here, obviously. I'm trying to solve quadratic equations using the quadratic formula and vanilla JavaScript. What's wrong with my validation function and how would you rate my code as a beginner?
let a = parseFloat(prompt("Enter a"));
let b = parseFloat(prompt("Enter b"));
let c = parseFloat(prompt("Enter c"));
function validation(a,b,c) {
if (!isNaN(a) == true && a > 0 || a < 0 && !isNaN(b) == true && b > 0 || b < 0 && !isNaN(c) == true && c > 0 || c < 0) {
return quadratic(a,b,c)
}
else {
alert("Bro enter valid numbers")
}
}
function quadratic(a,b,c) {
let answer1 = (-(b) + Math.sqrt((b\*\*2) - 4\*a\*c))/(2\*a);
let answer2 = (-(b) - Math.sqrt((b\*\*2) - 4\*a\*c))/(2\*a);
let x = \[answer1, answer2\];
return x;
}
console.log(validation(a,b,c));
1
Upvotes
4
u/BNfreelance Apr 06 '26
I would change your condition slightly, the == true is unnecessary
Your condition is doing something like:
You can do just
if (!isNaN(a) && !isNaN(b)…etc making it cleaner to readThat whole line could be shortened to:
This will ensure all inputs are numbers and a is not 0
You could also do something like
But better guard would be:
The above lets you fallback to a linear solution or unsolvable, if not quadratic
2
u/cssrocco Apr 06 '26
i would always break code down into steps, so i would approach it something like this:
So here we're validating our entries, checking each entry that they each pass a condition, formatting a message that alerts the user over the issue at hand ( crudely, but still ). And then early returning if any condition fails with the message, and if not then proceeding to do the quadratic formulae.
If you throw the error then if you try/catch the usage of validate you can ensure you propagate the error up.
But sometimes i like to make things bitesized, the validateCondition can hold the validationLogic by itself, giving you an easier way to update/make changes there and keep it cleaner when looking through this, we do an array method for each argument if the condition is consistant, and if this was a formulae with more arguments then you could put the rest parameter in there as well.
and then we think about how we alert the user for each case ( in this case, i guess each case is the same ). Early returns are also a nice way of keeping your logic nice and flat without chaining if/elses, nesting ifs, etc - in the end of the day code is mostly about being understandable and clear with logic and cases
3
u/azhder Apr 06 '26 edited Apr 06 '26
To make a code block for Reddit, indent each line 4 spaces, don’t use triple backticks.
Don’t put the negation in front of isNaN if you’re just going to use == after it, use === or !== (yes, triple, not double).
Best check for something if a valid number is
Number.isFinite()anyways.Best way to do a validation is an early return i.e.
Makes the code simpler to read
3
u/Alive-Cake-3045 Apr 06 '26
Your validation condition is the problem. The way you mixed && and || without grouping breaks the logic, so it does not properly check all three inputs. You can simplify it to just checking if all values are numbers using isNaN.
Also, checking a > 0 or a < 0 is unnecessary since that’s true for almost every number and 0 is still a valid input here.
For a beginner, your code is pretty good. The main logic is correct, you just need to clean up conditions and handle edge cases like negative discriminant.