Return an index of an element

Just a common developer like you ! Let's learn together and lift up each other.
Welcome back ! without wasting time let us focus on problem.
→ Given an array and number (n). You have to find the (the index of an element) in it and return it. If number is not present in an array then it should return -1
Now let’s break problems into smallest steps ⛓️💥 :
Given an array (
here my mind pops up this [ ]) and number (n) means user will provide me an array and a number.You have to find the (
the index of an element) means I have to go through each element of arraysReturn index of element (
remember array index starts with 0) if element is present in arrayIf not then we are returning
-1
Now build logic 🤔 :
I need function because I am performing set of codes on every user input. So I have to tell Java script its function thus I am using function keyword. Giving it suitable name findIndex so another developer can understand.
function findIndex(){ }- Am I receiving from user ? Yes an array and a number. Go back and revise smallest step #1
Giving two parameters to a function so it should process input from user. Calling it userArray, userNumber
function findIndex(userArray,userNumber){
}
Visual representation of my logic

Now let’s iterate through an array and include each elements
function findIndex(userArray,userNumber){
/* how to use for loop ? increment
initialization, breaking condition if receives false then loop will end, increment */
//need for loop
for(let i=0; i<userArray.length;i++){
//i should go till array length not more than that
// this condition will give me each and every element in userArray[i]
}
Here we have one condition

If number found :
function findIndex(userArray,userNumber){
for(let i=0; i<userArray.length;i++){
//if number is matching to array element
if(userArray[i] == userNumber)
{
return i // function will end after returning index
}
}
}
if number not found : Now you will think we will add in else block return -1 so what happen first element of array is not matched with userNumber and it will fall under else
Remember return -1 will stop function execution. so your code is broken it will always return -1
function findIndex(userArray,userNumber){
for(let i=0; i<userArray.length;i++){
//if number is matching to array element
if(userArray[i] == userNumber)
{
return i // function will end after returning index
}
else
{
return -1 // this will always return -1 except your output is like findIndex([1,2,3],1)
}
}
}
We have to return -1 after going through each element of array. So basically here whenever we are done looping through array element. so we are returning -1 at very last line of function / for loop is ending
function findIndex(userArray,userNumber){
for(let i=0; i<userArray.length;i++){
//if number is matching to array element
if(userArray[i] == userNumber)
{
return i // function will end after returning index
}
}
return -1
}
yay ! 🥳 great we did it. Now lets call function and give some input as a user.
function findIndex(userArray,userNumber){
for(let i=0; i<userArray.length;i++){
//if number is matching to array element
if(userArray[i] == userNumber)
{
return i // function will end after returning index
}
}
return -1
}
findIndex([2,5,6,7],7) // output 3
findIndex([2,5,6,7],88) // output -1
Tip : IndexOf( )
Do you know there is inbuilt function indexOf( ) in JS which will give you the same result.
const arr = [1, 4, 6];
console.log(arr.indexOf(6)); // Output: 2
console.log(arr.indexOf(8)); // Output: -1
What we learn :
This problem teaches how to iterate through an array and search for an element.
You can solve it manually using a loop (for better understanding of logic) or quickly using .indexOf() for cleaner code.
What next ?
Practice the same problem without reading this ! Remember building muscle memory is necessary. I know you know everything but there is a difference in knowing and doing
✨ Keep Coding, Keep Learning ✨ If you learn something give this a like and share !




