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?
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.
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:
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;
}
}
}
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.
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. returnALWAYS 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 -1after 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
}
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
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!!
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.