Ad
  • Custom User Avatar

    Note that A[A.length] is undefined too.

  • Default User Avatar

    i is coming up as undefined because you're probably trying to use it outside of your for loop. While normally it's not a problem, by declaring it as let i=0 in your loop construction, you are limiting the scope of i to just that loop.

    For example:

    function findOdd(A) {
      for (let i = 0; i <= A.length; i++){
      
      }
      return i; //i is undefined!
    }
    

    The above code will result in an error with i being undefined.

    This code will not:

    function findOdd(A) {
      for (i = 0; i <= A.length; i++){
      
      }
      return i;
    }
    

    The above will declare i as a global variable, which isn't the best thing to do, but the code will work.

    The best practice is probably to use var like the following:

    function findOdd(A) {
      for (var i = 0; i <= A.length; i++){
      
      }
      return i;
    
    }
    

    The above code will limit the scope of i to just this function. So, if you were to check the value of i outside of this function, it would be undefined.

    let i - is block scoped. That is, i will only exist inside the current block of code, and any blocks within that block.
    var i - is function scoped. i will only exist inside the function in which it is declared.

  • Custom User Avatar

    I don't know, and that's not important. Tests should generate positive or negative numbers in random tests too, that should be easy to fix.

  • Custom User Avatar

    Thanks for the quick and thorough reply! I'm quite new to this (also not sure why this reply text area looks like the coding input window.. I'll figure it out sooner or later). Anyhow, yes I'm writing my answer in the solution window and not for the tests, and regarding the reference to 'A', I'm assuming A stands for each of the given test arrays that are plugged into the funtion, so After changing all my references to 'A' in my solution code (as opposed to 'array') that problem seems to be mitigated, but now when I do this:

    function findOdd(A) {
    for (let i = 0; i <= A.length; i++)
    //more code follows
    }

    'i' comes up as undefined. I don't recall ever needing to declare 'i' when iterating through an array.. I tend to get stuck on the most rudimentary issues so this could be one of those times. Thanks for any insight you may have into this(!)

  • Default User Avatar

    You're writing your solution in the sample test area, or you're writing the solution in the solution area? The 'A' variable doesn't exist until it's passed into your function.

    I guess I'm having trouble understanding what you're doing. When you write a function, you set the variable names for the values that are passed into your function. In javascript, it looks like this:

    function blah(a, b, c){
      return a+b+c;
    }
    

    And then that function can be called from elsewhere. Like this:

    temp = blah(3, 4, 5); //temp will now equal 12
    console.log(temp); //prints 12 to console
    

    In this example, when 3, 4, and 5 are passed to the blah function, the code inside the function will recognize that a == 3, b == 4, and c == 5
    However, if outside your function, you try to call on one of those variables, they won't exist. So if you try this outside of your function code:

    console.log(a);
    

    It won't print 3 to the console, it will print undefined, because a doesn't exist outside of the function code.

    You shouldn't be writing in the sample text box unless you're trying to write your own tests. If you're just trying to come up with a solution, you should just stick to the solution box. The starter code looks like this:

    function findOdd(A) {
      //happy coding!
      return 0;
    }
    

    Outside of the curly brackets of the function, A does not exist.

  • Custom User Avatar

    The way the test module is set up is confusing. I'm writing a solution in the block for the given function with a parameter of 'A' and avoided changing anything that was already present that might disrupt the test. Without changing anything, and assuming 'A' to mean 'array', I'm writing what seems to be a workable solution, but then it doesn't recognize what 'A' even is and the test fails.. Has anyone else had this problem or know what I might be doing wrong?