Given an array (myArray), write a JS program to find the index of the first boolean value in myArray using "findIndex()" method. Note: If the value is not present in the array, findIndex() returns "-1".Input :The first line of input will contain an array (myArray).Output:
The output should be a single line containing the index or -1.
input1:[ 'a', true, 'v', 5 ]
output1:1
input2:[ 'Oxygen', 'Nitrogen', 'Carbon', 'Hydrogen' ]
output2:-1
function getIndex(myArray) {
 return myArray.findIndex(element => (element == true || element == false));Â
 }
const myArray =['a', true, 'v', 5 ];
console.log(getIndex(myArray));Â
const myArray1=[ 'Oxygen', 'Nitrogen', 'Carbon', 'Hydrogen']
console.log(getIndex(myArray1));Â
Comments
Leave a comment