Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Note that
A[A.length]
is undefined too.i
is coming up as undefined because you're probably trying to use it outside of yourfor
loop. While normally it's not a problem, by declaring it aslet i=0
in your loop construction, you are limiting the scope ofi
to just that loop.For example:
The above code will result in an error with
i
being undefined.This code will not:
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:The above code will limit the scope of
i
to just this function. So, if you were to check the value ofi
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.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.
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(!)
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:
And then that function can be called from elsewhere. Like this:
In this example, when
3
,4
, and5
are passed to theblah
function, the code inside the function will recognize thata == 3
,b == 4
, andc == 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:
It won't print
3
to the console, it will printundefined
, becausea
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:
Outside of the curly brackets of the function,
A
does not exist.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?