Good day.
I have recently started learning about array iteration method. I was doing a challenge question and I cannot seem to understand why I cannot solve it. I don’t know where I’m going wrong (the output is displaying undefined. Please assist. I need your pearls of wisdom. The challenge is as follows:
/* Create a function that takes an array and a string as arguments
and returns the index of the string.*/
for example,
//findIndex([“hi”, “edabit”, “fgh”, “abc”], “fgh”) ➞ 2
My solution is as follows:
function findIndex(array,string){
let theIndex = arr.forEach( function(val,index){
if(val===string){
return index;
}
} )
return theIndex
}
console.log(findIndex([“hi”, “edabit”, “fgh”, “abc”], “fgh”))