Domanda

Is there is a way to compare strings in JSLint?

I have written the following code which is evaluated by JSLint:

if(('SIVA') === 'SIVA'){
    Result = 1000;
    return;
}

The above code works for javascript but I am getting the following error when evaluated by JSLint: "Weird Relation"

How to rectify this issue?

È stato utile?

Soluzione

The solution might be quite easy.

Your backend code is writing a string into JavaScript, right? So instead of this:

if( ( 'SIVA' ) === 'SIVA' ) {
  result_int = 1000;
  return;
}

Which on the backend looks like this:

if( ( '<%key_code%>' ) === 'SIVA' ) {
  result_int = 1000;
  return;
}

Do this on the backend template:

var key_code = '<%key_code%>';

if( key_code === 'SIVA' ) {
  result_int = 1000;
  return;
}

Which will be filled out on the client as such:

var key_code = 'SIVA';

if( key_code === 'SIVA' ) {
  result_int = 1000;
  return;
}

Both the template and the code delivered to the client should pass JSLint.

Altri suggerimenti

Not real sure what you're trying to do here. You're comparing a static string to a static string with ('SIVA') === 'SIVA', which gives a weird relation.

If you compare 'SIVA' to 'SIVA', it'll always be true. Why use an if to see if true is true? That's weird! That's the reason JSLint reports "Weird condition". ;^)

I'm assuming you wanted one of those SIVAs to be a variable, which I change in the code below.

Here's code similar to what I think you're doing that passes JSLint.

/*jslint sloppy:true, white:true, browser:true */
var Result, sivaValueToCheck;

if(sivaValueToCheck === 'SIVA'){
    Result = 1000;
    // return;  // don't exit early.  configure your else.
}    else    {
    window.alert('do the logic you wanted to skip before');
}

Note that you have to declare Result and you can't simply return if you want to pass JSLint. You can debate the usefulness of the single return rule, but the idea is that a single return makes for easier to follow code.

Javascript accepts if('a'== 'a') but JSLint doesn't allow it due to weird_relation property. If weird_relation property is removed from messages and if it is removed from the method where it is used, it won't show any error.I did that and it started working.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top