I can't figure out how to make my code return the first odd number and if there are no odd numbers to return undefined. What am I doing wrong/need to research? The 3 requirements I have are;
1 Return the first odd number in an array of odd numbers
2 Return the first odd number when it is the last number in the array
3 Return undefined when there are no odd numbers
function firstOdd(arg){
for (let i=0; i <= arg.length; i++)
if (i % 2 !== 0) {
return(i)
} else {
return undefined;
}
}
Thank you in advanced for being kind with me.
for (let i=0; i <= arg.length; i++) if (i % 2 !== 0) return i;is the whole body of the function; nothing else required, not evenreturn undefinedfunction firstOdd(arg){ for(const v of arg) if(v&1) return v }