JSLint: Expected 'else {if' and instead saw otherwise if

I am trying to check the prompt in the main JavaScript function. Basically, it asks for a number from 10 to 20. If the value does not fall into this range or is not a number, it should give an error message and repeat the request. If 0 is entered or the cancel button is pressed, it must completely stop the procedure.

I know this is very bad code, but it works great for me.

The problem I am facing is when I try to test it in JSLint. I get the following error:

Expected 'else { if' and instead saw 'else if'.
        } else if (amtEntered >= 10 && amtEntered <=20) {

If I uncomment the instruction return 0, the error has disappeared.

var amt;    
var inputError = false;

do {
    if (inputError) {
        alert ("You have entered an error. \n Please try again.");
    }
    amt = prompt("Enter amount between 10 and 20: ","");
    amt = Number(amt);
    if (isNaN(amt)) {
        inputError = true;
    } else if (amt === null || amt === 0) {
        // If amt is blank or cancel button pressed then exit loop and exit function.
        inputError = false;
        return 0;
    } else if (amt >= 10 && amt <=20) {
        inputError = false;
    } else {
        inputError = true;
    }
}
while (inputError);

Thank you for any tips or tricks for fixing this part of the code.

+4
4

, , , var alert prompt. ( "" var, , jslint browser:true window alert prompt ).

-, , ( ), return , . wrapMe(), , JSLint return, unexpected return .

-, return, , , "block.disrupt" ( JSLint ), JSLint.

, , . ( JSLint) else a return. if. , JSLint , , , else - , JSLintian.

. else block.disrupt, if, , .

, break, . break; return 0;, ​​, , . ( .)

, , , JSLint - . .

/*jslint sloppy:true, white:true, browser:true */

function wrapMe()   {
    var amt, inputError = false;

    do {
        if (inputError) {
            window.alert ("You have entered an error. \n Please try again.");
        }
        amt = window.prompt("Enter amount between 10 and 20: ","");
        amt = Number(amt);
        if (isNaN(amt)) {
            inputError = true;
        } else {
            if (amt === null || amt === 0) {
                // If amt is blank or cancel button pressed then exit loop and exit function.
                inputError = false;
                return 0;  // BLOCK DISRUPTION!!! (Not sure why that felt Scott Pilgrim-y)
            } 
            // If no disruption, continue.
            if (amt >= 10 && amt <=20) {
                inputError = false;
            } else {
                inputError = true;
            }
        }
    }
    while (inputError);
}

, else if - if.

+3

, , if :

if (isNaN(amt)) {
    inputError = true;
} else {
    if (amt === null || amt === 0) {
        // If amt is blank or cancel button pressed then exit loop and exit function.
        inputError = false;
        return 0;
    } else {
        if (amt >= 10 && amt <=20) {
            inputError = false;
        } else {
            inputError = true;
        }
    }
}

, JSLint, . , , - .


, , JSLint , , :

if (condition)
    do_something();

- :

if (condition)
    do_something();
    do_something_else();

. , , , .

, , return, .

, if ( ), , else. if (, return), else ( else) ( else if)

stmt('if', function () {
    var paren = next_token;
    one_space();
    advance('(');
    step_in('control');
    no_space();
    edge();
    this.arity = 'statement';
    this.first = expected_condition(expected_relation(expression(0)));
    no_space();
    step_out(')', paren);
    one_space();
    this.block = block('if');
    if (next_token.id === 'else') {
        if (this.block.disrupt) {
            next_token.warn(this.elif ? 'use_nested_if' : 'unnecessary_else');
        }
        one_space();
        advance('else');
        one_space();
        if (next_token.id === 'if') {
            next_token.elif = true;
            this.else = statement(true);
        } else {
            this.else = block('else');
        }
        if (this.else.disrupt && this.block.disrupt) {
            this.disrupt = true;
        }
    }
    return this;
});

unnecessary_else , , :

if (condition) {
    return 0;
} else {
    do_something();
}

:

if (condition) {
    return 0;
}
do_something();

use_nested_if , , , , return. , , .

, , , , , , . , tolerate_non_nested_if . , , .


, - if, else, ( if/else, ).

, , , :

if (isNaN(amt)) {
    inputError = true;
} else {
    if (amt === null || amt === 0) {
        // If amt is blank or cancel button pressed then exit loop and exit function.
        inputError = false;
        return 0;
    } // no else needed here.
    if (amt >= 10 && amt <=20) {
        inputError = false;
    } else {
        inputError = true;
    }
}
+5

JSLint , , ( ) .

var amt,
    fail = true;
// would use `while (amt = prompt("Enter amount between 10 and 20: ", ""))` personally
amt = prompt("Enter amount between 10 and 20: ", "");
while (amt) {
    amt = +amt; // to Number
    if (10 <= amt && amt <= 20) {
        fail = false;
        break;
    }
    alert("You have entered an error. \n Please try again.");
    amt = prompt("Enter amount between 10 and 20: ", "");
}
if (fail) {
    return 0;
}
+1
source

This may have something to do with return 0;; a is elsenot required when this condition is achieved. Of course, since it returnis located in else if, the correction is less simple, and the proposed correction does not really make much sense.

Fortunately, you can simplify!

var amt;
var inputError = false;

while (true) {
    if (inputError) {
        alert ("You have entered an error. \n Please try again.");
    }

    inputError = true;

    amt = prompt("Enter amount between 10 and 20: ");

    if (!amt) {
        return;
    }

    amt = Number(amt);

    if (isNaN(amt)) {
        continue;
    }

    if (amt >= 10 && amt <= 20) {
        break;
    }
}

... assuming you have enough checks to not just do this:

if (!amt) {
    return;
}

amt = +amt;
inputError = !(amt >= 10 && amt <= 20); // NaN will always compare as false here
0
source

All Articles