[Solved] Multiple if statements and then else Javascript [closed]


I can’t tell you what to do to make it “work”, since your desired result is pretty unclear, but a number of problems in your code jumped out at me as I skimmed it top to bottom. Some will still run but give results you aren’t expecting, but at least one problem will cause an error and stop the function running. So:

Most of your comments are either redundant and could be removed, or could be made redundant and removed if you made your variable names more descriptive.

The statement

if (isNaN(document.inverse.sin.value) == true || ...)

could be made simpler because you’ve already declared a variable sin that is equal to document.inverse.sin.value and the == true part is redundant. Just say

if (isNan(sin) || isNan(cos) || isNan(tan)) {

All of your “reset” statements like var sin = null; are both wrong and pointless because (a) using var means you are attempting to redeclare the variables (I think JS just ignores this), so you should just say sin = null;, but in any case (b) they’re local variables within your trigI() function so they’re all about to disappear anyway when the function returns. If you are trying to clear the fields on the screen you need to say document.inverse.sin.value = "";, but you don’t want to annoy the user by clearing all the fields just because one of them was invalid. Tell them which field had a problem, set focus to that field, and let them correct it themselves.

Most of your if statement test something like this:

if (document.inverse-cb1.checked == 'false'){

Which will never be true because you are comparing the .checked property that is a boolean equal to true or false with the string 'false'. You need to say

if (document.inverse-cb1.checked == false){    // note: no quotes
// OR, even better
if (!document.inverse-cb1.checked){

What is sup.sup() supposed to do? sup is a variable that you’ve set to -1. sup() is a non-existent function that in any case you shouldn’t be trying to call as a method of a number. Fix this first, because I think this is what is stopping the function returning.

EDIT: I noticed another big problem:

document.inverse.inverse-cb1.checked

You can’t use the “dot” object notation for properties that don’t follow the rules for JS identifier naming, and JS identifiers can’t have a “-” in them. So although “inverse-cb1” is a valid property name you have to use the array-style bracket [] syntax to access it:

document.inverse["inverse-cb1"].checked

Or you could just remove the “-” from both the html and your JS.

5

solved Multiple if statements and then else Javascript [closed]