When searching through an array of strings, a loop will return false
if it encounters a string that does not match the criteria being looked for.
If no mismatches are found in the array, it should return true
. However, even when there are no errors in the array, it continues to return false
.
I have attempted various methods such as using indexOf
, for loops, and while loops, but have had no success.
function checkBrackets() {
var testArr = ['()', '{}', '()']
/* Method 1 --- returns false even when the parentheses are correct,
likely due to indexOf only finding the first matching element */
if (testArr.indexOf("()") == -1 || testArr.indexOf("{}") == -1 ||
testArr.indexOf("[]") == -1) {
return false
} else {
return true
}
/* Method 2 --- for loop. Also returns false despite all elements
complying with the conditions. It behaves strangely and I'm unsure why */
for (i = 0; i < testArr.length; i++) {
if (testArr[i] !== "()" || testArr[i] !== "{}" || testArr[i] !== "[]") {
return false
}
}
return true
}
checkBrackets()