简体   繁体   中英

Javascript: Comparing output of function to array within a conditional

I'm using this code to compare the output of a function (from an array of args) to an array of expected outputs, and log the results in an array (called results[], default filled with false values, hence why there is no else conditional).

        for(i=0;i<args.length;i++){
          console.log('in for');
          if(f(args[i])==expected[i]){
            console.log('in if');
            results[i] ='true';

          }
        }

when I run the function, 'in for' appears in the console args.length times as expected. However, 'in if' never appears (even when the conditional is true). What's the issue?

EDIT: going to provide some more details I'm making a site where people can input js code to solve a certain problem, and the website will get the code from the textarea, run the code, and compare it to the output.

Below is my extremely messy test function, that takes the parameter of the code the user inputted into the text area:

 function test(inputcode){
      var passed = inputcode;
      var allcorrect = true;
      try{
        eval( "var f = " + passed);
        var args = ["pogchamp","progChamprogCHAMPchamp","pro2GCHAMPCprogH9AMPprogchamp","progchamp19","1pro2champ2prog","progCHamp","Prog Champ"];
        var expected = ['false','false','true','true','true','true','true'];
        var results=['false','false','false','false','false','false','false'];

        for(i=0;i<args.length;i++){
          console.log('in for');
          console.log(f(args[i]));
          console.log(expected[i]);
          if(f(args[i])==expected[i]){
            console.log('in if');
            results[i] ='true';

          }
        }
        for(i=0;i<results.length;i++){
         if(results[i]==false){
          allcorrect=false;
        }
      }

    }
    catch(err){
      alert("Something went wrong. Check your code and resubmit.");
      console.log(err.message);
    }

  }

The output of the code when i run it in chrome is:

in for
false
false
in for 
false 
false 
in for 
false 
true 
in for 
false 
true 
in for 
false 
true 
in for 
false 
true 
in for 
false 
true

The input function (f()) in this case just returns false, and does nothing more.

function foo(str) {


return false; //auto-generated
}

There's a big difference between false and 'false' ; the former, being returned from the test function, is a boolean and the latter is a string.

For example:

false == 'true'  // false (okay)
false == 'false' // false (hmmm)

Change the expected array definition to:

var expected = [false,false,true,true,true,true,true];

You are comparing a string 'false' to the boolean false . Return a string from your function or change the array to the boolean value and it will work.

A non-empty string (even if it is 'false' ) is always truthy and will return false when compared to false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM