1

Goal: 1) Create a string using the ID attributes of several elements. 2) Push that string to an array. 3) Check to see if the string in the array forms a particular word -- in this case, "idaho".

Problem: I can only seem to verify that a particular character is in the array. If I use more than one character in the "value" of "$.inArray", it returns false.

Question: How can I verify that the string in the array forms the word "idaho" instead of just verifying that the string contains an "i" or "d" or "a", etc.?

This FIDDLE works, but only because it's looking for an "i" instead of "idaho".

I've done a lot of research on this, but can't get it to work. Thanks for helping a noob!

<div class="pieces" id="i"></div>
<div class="pieces" id="d"></div>
<div class="pieces" id="a"></div>
<div class="pieces" id="h"></div>
<div class="pieces" id="o"></div>

 var piecesArray = [];
 var ids = $('.pieces').map(function() {
   piecesArray.push(this.id);
 });
 piecesArray = piecesArray.join('');
  if ($.inArray('i', piecesArray) !==-1){
    alert(piecesArray);
  }
2
  • 2
    piecesArray === 'idaho'? Note that after using join you have a string not a real array. Commented Jan 3, 2015 at 21:42
  • Why are you building an Array instead of making your String directly? Commented Jan 3, 2015 at 21:46

5 Answers 5

2

You can get the current word directly in one go:

var pieces = $('.pieces').map(function() {
    return this.id;
}).get().join('');

and then just check that it matches the desired word:

if (pieces === 'idaho') 
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you just compare your joined array with a string?

var piecesArray = [],
    word;

$(".pieces").each(function() {
    piecesArray.push(this.id);
});

word = piecesArray.join("");

if (word === "idaho"){
    alert(word);
}

Comments

0

Why must you use $.inArray, you joined the pieces into a string, all you need for the alert to work is to replace :

if ($.inArray('i', piecesArray) !==-1)

with:

if (piecesArray == "idaho")

http://jsfiddle.net/q3x1gg9b/2/

Comments

0

Why don't you simple create a string from the start:

 var piecesString = "";
      var ids = $('.pieces').map(function() {
            piecesString += this.id;
       });
    if (piecesString == "idaho"){
      alert(piecesString);
    }

Here's a fiddle...http://jsfiddle.net/q3x1gg9b/4/

Comments

0

You are looking to see if a substring exists within the given string, rather than an element (character) exists in the array (string). The inArray() function searches for a single character in the string. Passing a string to the function forces the function to assume the targeted array is an array of strings, which it is not -> returns false.

Instead you should use the string.indexOf() function to check to see if the string contains a given substring.

if(piecesArray.indexOf('idaho') >= 0) { 
    // idaho is in the string! 
}

If the substring is in the targeted string the function returns the staying index of the substring. In this case since the substring is the string it would return 0.

Hope this helps!

2 Comments

This doesn't use inArray() function... Sorry to divert from post title. But I do not think it is possible to achieve a solution using inArray()
this is what we call an "XY Problem" - the OP has a problem with inArray, but that's because inArray isn't the right tool for the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.