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 matching strings in a String array

From Dev

Finding the longest string in an array of Strings

From Dev

Finding a part of string in an array of strings

From Dev

Finding all strings containing the substring in an array of string

From Dev

Finding all strings containing the substring in an array of string

From Dev

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

From Dev

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

From Dev

Finding unique strings in an array

From Dev

Finding similar strings in array

From Dev

Finding duplicate strings in array

From Dev

Splitting a string and extracting specific strings to array

From Dev

Splitting a string and extracting specific strings to array

From Dev

Finding specific values in an array

From Dev

Finding specific numbers in an array?

From Dev

Finding for string in an Array

From Dev

Finding the shortest string in the array

From Dev

Regex for finding a specific pattern in a string

From Dev

Finding ranges of specific character in a string

From Dev

Finding specific number inside a string?

From Dev

finding specific indices with pointer array

From Dev

Finding index of string in string array

From Dev

Finding text between two specific characters or strings

From Dev

Regex for finding specific strings with random center

From Dev

finding a specific array element and remove the container array

From Dev

array_search not finding string?

From Dev

Finding a string in a directory listing array

From Dev

Finding an index of an element in a string array

From Dev

Finding an undefined with first string in the array

From Dev

finding words from an array, in a string

Related Related

  1. 1

    Finding matching strings in a String array

  2. 2

    Finding the longest string in an array of Strings

  3. 3

    Finding a part of string in an array of strings

  4. 4

    Finding all strings containing the substring in an array of string

  5. 5

    Finding all strings containing the substring in an array of string

  6. 6

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

  7. 7

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

  8. 8

    Finding unique strings in an array

  9. 9

    Finding similar strings in array

  10. 10

    Finding duplicate strings in array

  11. 11

    Splitting a string and extracting specific strings to array

  12. 12

    Splitting a string and extracting specific strings to array

  13. 13

    Finding specific values in an array

  14. 14

    Finding specific numbers in an array?

  15. 15

    Finding for string in an Array

  16. 16

    Finding the shortest string in the array

  17. 17

    Regex for finding a specific pattern in a string

  18. 18

    Finding ranges of specific character in a string

  19. 19

    Finding specific number inside a string?

  20. 20

    finding specific indices with pointer array

  21. 21

    Finding index of string in string array

  22. 22

    Finding text between two specific characters or strings

  23. 23

    Regex for finding specific strings with random center

  24. 24

    finding a specific array element and remove the container array

  25. 25

    array_search not finding string?

  26. 26

    Finding a string in a directory listing array

  27. 27

    Finding an index of an element in a string array

  28. 28

    Finding an undefined with first string in the array

  29. 29

    finding words from an array, in a string

HotTag

Archive