Answer to Question #241621 in HTML/JavaScript Web Application for rasi

Question #241621

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


1
Expert's answer
2021-09-24T03:24:57-0400
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)); 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS