Ad
  • Default User Avatar

    Thanks again!

  • Custom User Avatar

    Well, in your first example, you aren't calling the functions, so the result is incorrect. (I'm not sure why you are assigning variables in the second example.)

    In your test case above, it will always fail if your code is correct. This is because you are passing false into the _if function, which will mean the second function is evaluated. However, your second function is returning false, which means the final value returned to Test.expect is false.

    // note: I labeled the functions to make it easier to see them
    Test.expect(
        _if(
            false,                // false -> means run fnFalse
            function fnTrue() {   // ignored
              return true;
            },
            function fnFalse() {  // run
              return false;       // returns false
            }
       )                          // returns result of fnFalse, which is false
       
                                  // effectively this becomes Test.expect( false )
    );
    

    This is why your test case failed, while the submission passed.

    So, why did the first attempt succeed? It's because you were returning the functions themselves, rather than their result, which looks like this:

    Test.expect(
        _if(
            false,                // false -> means run fnFalse
            function fnTrue() {   // ignored
              return true;
            },
            function fnFalse() {  // *returned*, but never run
              return false;
            }
       )                          // returns fnFalse itself, which is a Function object
       
                                  // effectively this becomes Test.expect( function(){} )
                                  // Test.expect passes if it is passed a truthy value. 
                                  // As it turns out, all Objects are truthy.
                                  // So, the end result of this becomes Test.expect( true )
    );
    

    Now that you've solved the kata, you can go back and see the other solutions from other users. Hopefully this helps you! If that all makes sense, feel free to mark this issue as resolved.

  • Default User Avatar

    Sure. I was testing in JavaScript. my code was:

    function _if(bool, func1, func2) {

    var result = (bool) ? func1 : func2;

    return result;

    }

    and my test was:
    Test.expect(_if(false, function(){ return true; }, function(){return false;}));

    The output was always a pass when testing, but failing upon submission. A few moments ago, I got it too work.

  • Custom User Avatar

    What language are you testing, and do you mind posting your code?

  • Default User Avatar

    Hi OverZealous, no I haven't been able to. It seems like there could be a mistake on my end or there is something a bit funny with the script.

    When I declare a variable within the function, and then reuse it later, it doesn't seem to be defined. I am currently reading some of the documentation to see if there was something I missed.

  • Custom User Avatar

    @matt6frey, Were you ever able to get the solution to submit?

  • Default User Avatar

    All right, thanks. I will see how it goes. Cheers!

  • Custom User Avatar

    It might be a one-off quirk. If you can, copy your solution as it is, reload the page, paste your solution back in and try again.

    Sometimes this fixes the issue for me. (Copying your solution ensures that you won't lose your work if something goes wrong.)

  • Default User Avatar

    I am trying to submit a solution to the Kata "The 'if' function" and I have a solution that passes the tests, but not on submission. My error is: Failed to send to server. Check your internet connection.

    My connection is fine. Can anybody help me out? Thanks!