I implemented a linear search algorithm using array.forEach(), but I can't figure out why it is not returning the index of the value I am searching for:
function linearSearch(array, value) {
array.forEach((currentValue, index) => {
if (value === currentValue) {
return index;
}
});
return -1; //if not found, return -1
}
const arr = [1,2,3,4,5];
const found = linearSearch(arr, 3);
console.log(found); //should be 2?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Linear Search</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>