FAQ: Code Challenges: Intermediate JavaScript - findMyKeys()

This community-built FAQ covers the “findMyKeys()” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise findMyKeys()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi I’m a bit stuck on this code challenge. I’m not sure why my function declaration isn’t correct - it outputs undefined:

const findMyKeys = arr => arr.findIndex(item => item === ‘keys’) //This is the correct one resulting in 4

const findMyKeys =(arr)=>{
arr.findIndex(item => item === ‘keys’)}; //This was my answer(only difference is adding () around the parameter and {} brackets

//The rest of the code is here for context: //
const randomStuff = [‘credit card’, ‘screwdriver’, ‘receipt’, ‘gum’, ‘keys’, ‘used gum’, ‘plastic spoon’];


The question was to create a function that finds the index of ‘keys’ in randomStuff. Is there a certain rule of how functions can be notated that I’m missing, I thought it was acceptable to put () around parameters and {} brackets for multi-line returns.
I recognize its shorter syntax as its only a single parameter and a single line return but does that mean you’re only allowed to use the shortened syntax?

Any help would be really appreciated

When a block is used to wrap the function, it must contain a return. A single parameter may be written with or without parens. No parameter or multiple parameters must be in parentheses.

foo = bar => func(bar)

foo = (bar) => func(bar)

foo = bar => {
    return func(bar);

foo = (bar, func) => func(bar)

foo = (bar, func) => {
    return func(bar);

Ah ok, that makes sense now. Many thanks!

I also struggled with interpreting some of the MDN definitions for iteration methods. A few of them have square brackets when describing the callback function usage:

arr.findIndex (callback(element[, index[, array]])[, thisArg])
arr.every(callback[, thisArg])

I made do with some examples, but what are the square brackets meant to signify in this case?

They are meant to signify optional arguments. The one out front that is not in brackets is a required argument.

arr.findIndex (callback(element))

1 Like

I can’t understand my error in this code :
function findMyKeys(array) {
for (let i = 0; i < array.length; i++) {
if (array[i] === ‘keys’) {
return i;
} else {
return -1;

Are you getting an error in the console, or an SCT error message?

In your code, you are only able to check the first item in the array. If the first item is equal to ‘keys’ you return its index, but if it isn’t, you return -1. Since both conditions include a return statement, your function is terminated after the first element is tested. You need to refactor your code, so that you can iterate through the array until you find your ‘keys’. Might consider using the built in methods .indexOf() or findIndex(). Your way will work with a little tweaking, but the built in methods are worth consideration.

Good luck && happy coding!

1 Like

The code editor is showing this error - “Your function should return the index in the array where ‘keys’ are located.”

1 Like

Why is the function terminated because of two return statements in different conditions ? Also, the if else statement is nested inside a for loop which iterates through the array.

Whenever we depend upon a loop to fully complete iteration of an object, we must not have a return in both clauses of an if statement.

Above, when keys is found, we can return its index; but, not anything else. Let the loop terminate on its own terms (after complete iteration) and if it reaches the end, then after the loop, return -1.


Ditto @mtf. After your first if condition is evaluated, one of the two code blocks will execute: the if block if the condition is true; the else block if the condition is false. Since both blocks have a return, the execution of the function is terminated. return ALWAYS sends control back to the line of code that called the function or method. As @mtf stated, you need to return the index of ‘keys’ if you find them, or return -1 after your loop has fully iterated over the entire array. Give it another try, and reply again if you’re still stuck. Your basic structure should resemble this:

function findMyKeys(array) {
  for(let i = 0; i < array.length; i++) {
    //if statement to determine if you've found 'keys'
      //return i
  //if your code reaches this point, you haven't found 'keys'
  //even after searching the entire array,
  //so you can return -1
  //no need for an else statement at all

Good luck && happy coding!!

1 Like

Thanks. The code worked.

1 Like

Thanks for helping. The code worked.

could anyone explain why this code isn’t passing this challenge? It’s not the most concise but does what the exercise is asking:

function findMyKeys(arr) {
  var location = []
  for (i=0; i < arr.length; i++) {
    if (arr[i] === 'keys') {
  if (location.length > 0) {
    return location
  } else {
    return -1

Ok, so I’ve been good and try my best to not look up answer, be patient and try to work the solutions out myself… 24 hours later I’ve clicked on the solution button, guess what? To see that my code was correct probably the first or second of my attempts all along but there’s probably some kind of system error, again! This is not the first or second I’ve came across :unamused: :weary:

I just wish this would stop happening !! and also while I’m here I might as well complain! Again, why is it that the system is always timing me out and cuz of that I’d always have to refresh the page!! so annoying!!

Did you get something like this? My code is throwing no errors, it logs the correct result, but Codecademy won’t tick it off:

const findMyKeys = arr => {
  for (i = 0; i < arr.length; i++){
    if (arr[i] === 'keys'){
      return i;
}  //prints 4

If the keys are in the array, your function should return the index at which they can be found. If they’re not in the array, your function should return -1 .

Thanks - well, yes keys was at index position 4 (should have said that) so it looks like my code works. I got past the gremlins by using .findIndex instead. But good to know this was ok.

1 Like

Be sure your function returns -1 when no keys found.