I wrote a program to find a specific value from an array and return it's position in the array.
function findBase(array, value) {
array.forEach(function (v, i) {
const length = Number(array.length) - 1;
if (v == value) {
console.log(i)
return i;
} else if (i == length) {
return false;
};
});
};
Input:
var a;
a = findBase([0,1,2,3], 2)
console.log(a);
Output:
2
undefined
Is there any solution that does not involve making a global scope variable?