Finding a specific string in array of strings

vsl0610

In an array of strings, a loop returns false if it finds that one of the strings is not what we are looking for.

If it doesn't find an unmatching string, the array is correct and it should return true. It keeps returning false even when the array has no "mistakes"

I have tried using indexOf, for loop and while loops, all of them unsuccessful.

function brackets() {
    var testArr = ['()', '{}', '()']

    /* Method 1 --- returns false even when the parenthesis are ok, I guess
    it's because the indexOf only searches for the first element that matches 
    the criteria */

    if (testArr.indexOf("()") == -1 || testArr.indexOf("{}") == -1 ||
        testArr.indexOf("[]") == -1) {
        return false
    } else {
        return true
    }

    /* Method 2 --- for loop. Same story, returns false, even when all   
    testArr[i] === any of the cases and none of them is !==, it behaves as if it was 
    false. I'm not sure why */

    for (i = 0; i < testArr.length; i++) {
        if (testArr[i] !== "()" || testArr[i] !== "{}" || testArr[i] !== "[]") {
            return false
        }
    }
    return true
}

brackets()
Luca Colonnello

In the second method you can use an AND operator to solve this problem.

function brackets() {
var testArr = ['()', '{}', '()'];

/* Method 2 --- for loop. Same story, returns false, even when all   
testArr[i] === any of the cases and none of them is !==, it behaves as if it was 
false. I'm not sure why */

for (i = 0; i < testArr.length; i++) {
    if (testArr[i] !== "()" && testArr[i] !== "{}"  && testArr[i] !== "[]") {
        return false;
    }
}
return true;
}

brackets();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

finding specific indices with pointer array

From Dev

Finding all strings containing the substring in an array of string

From Dev

Finding specific values in an array

From Dev

C# Finding specific strings in a sentence and storing in a multidimensional array

From Dev

Finding the longest string in an array of Strings

From Dev

Splitting a string and extracting specific strings to array

From Dev

Finding matching strings in a String array

From Dev

array_search not finding string?

From Dev

Finding index of string in string array

From Dev

What is the optimized way of finding and replacing specific set of strings in a lengthy string?

From Dev

Finding a string in a directory listing array

From Dev

Finding for string in an Array

From Dev

Finding unique strings in an array

From Dev

Finding all strings containing the substring in an array of string

From Dev

Finding ranges of specific character in a string

From Dev

Regex for finding a specific pattern in a string

From Dev

Finding an index of an element in a string array

From Dev

Finding specific numbers in an array?

From Dev

finding a specific array element and remove the container array

From Dev

Finding similar strings in array

From Dev

Finding text between two specific characters or strings

From Dev

Splitting a string and extracting specific strings to array

From Dev

Finding a part of string in an array of strings

From Dev

Finding specific number inside a string?

From Dev

Finding duplicate strings in array

From Dev

Finding the shortest string in the array

From Dev

Finding an undefined with first string in the array

From Dev

finding words from an array, in a string

From Dev

Regex for finding specific strings with random center

Related Related

  1. 1

    finding specific indices with pointer array

  2. 2

    Finding all strings containing the substring in an array of string

  3. 3

    Finding specific values in an array

  4. 4

    C# Finding specific strings in a sentence and storing in a multidimensional array

  5. 5

    Finding the longest string in an array of Strings

  6. 6

    Splitting a string and extracting specific strings to array

  7. 7

    Finding matching strings in a String array

  8. 8

    array_search not finding string?

  9. 9

    Finding index of string in string array

  10. 10

    What is the optimized way of finding and replacing specific set of strings in a lengthy string?

  11. 11

    Finding a string in a directory listing array

  12. 12

    Finding for string in an Array

  13. 13

    Finding unique strings in an array

  14. 14

    Finding all strings containing the substring in an array of string

  15. 15

    Finding ranges of specific character in a string

  16. 16

    Regex for finding a specific pattern in a string

  17. 17

    Finding an index of an element in a string array

  18. 18

    Finding specific numbers in an array?

  19. 19

    finding a specific array element and remove the container array

  20. 20

    Finding similar strings in array

  21. 21

    Finding text between two specific characters or strings

  22. 22

    Splitting a string and extracting specific strings to array

  23. 23

    Finding a part of string in an array of strings

  24. 24

    Finding specific number inside a string?

  25. 25

    Finding duplicate strings in array

  26. 26

    Finding the shortest string in the array

  27. 27

    Finding an undefined with first string in the array

  28. 28

    finding words from an array, in a string

  29. 29

    Regex for finding specific strings with random center

HotTag

Archive