FAQ: Code Challenges: Intermediate JavaScript - findMyKeys()


#1

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!


#2

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’];

console.log(findMyKeys(randomStuff))

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


#3

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);
}

#4

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?


#5

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

arr.findIndex (callback(element))

arr.every(callback)