1

4,5,6,7]; pin=3;

We got to search pin in hay.

Conventionally we loop through hay and check for pin( assume there is no native function called array.indexOf ).

How about,

  hay=hay.join(",");
  pin=","+pin+",";
  index=hay.indexOf(pin);

Any Suggestions please?

2
  • 2
    please reformat your question Commented Jun 13, 2011 at 6:11
  • 1
    even if this worked, you would have to add one and divide by two. and it assumes that you will only ever have 1 digit numbers. Which pretty much guarantees the length of the inputs to be reasonably restricted to 10 elements, which any loop over ten elements can be as inefficient as you want and will still be very fast. Don't overoptimize this problem. Commented Jun 13, 2011 at 6:22

3 Answers 3

1

Consider hay of [2,3,4] and a pin of 2... you'll be looking for ",2," in a string "2,3,4". Now you could add commas to the start and end of hay as well, but it's a bit ugly, isn't it?

There's then the problem of strings having variable lengths: consider an array of [1,222222,3,4]. When you look for 3, you'll end up with an inappropriate index because of the length of "222222". (Even in the case of only single digit values, you'll need to divide by 3.)

You've then potentially got problems when you start moving from integers to decimal values, which may be formatted differently in different cultures - possibly using a comma as the decimal separator. I don't know whether JavaScript uses the culture-specific separator by default, but that's part of the problem - you're suddenly having to consider aspects of the language/platform which have nothing to do with the task at hand.

Generally speaking, converting data into a string format in order to do something which doesn't really depend on the string format is a bad idea. It would be better to write a general-purpose indexOf method to perform the looping for you. (I'd be surprised if such a method didn't already exist, to be honest, but it's easy enough to write once and reuse if you need to.)

Sign up to request clarification or add additional context in comments.

5 Comments

I believe the OP is attempting to find the location of a string by concat'ing many strings together then using indexOf to find the location of the string. I am sure it's an invalid approach.
@jcolebrand: It looks like it's not string data to start with, unless I've missed something. But yes, either way it's a bad idea. (And I'd forgotten about the variable-length issue.)
you didn't miss anything, except the .join(',')
@jcolebrand: But that's concatenating many integers together - the string representations internally, sure, but he's starting off with integers, not strings.
~ not to pick nits, but we both know that in javascript it doesn't really matter when it comes to .join(',') .. Now as for the request proper, it was flawed to begin with. And now, off to questions that actually matter in the world, such as BCNF or 3NF ;)
1

Heck, assume there is no string indexOf, either.

var A=[11,7,9,1,17,13,19,18,10,6,3,8,2,5,4,14,20,15,16,12],
L=A.length, n=3;


    while(L>-1 && A[--L]!==n);

alert(L)

Comments

0

You don't need to use string in the middle, you can just loop through your array, if I understand your question right.

var hay = [1, 2, 3, 'Whoa', 'wheee', 5, 'needle'], needle = 'needle';

for ( var i = 0, len = hay.length; i < len; i += 1 ) {
    if ( hay[ i ] === needle ) {
        alert( "hay’s element number " + i + " is the needle!" );
        break;
    }

}

4 Comments

I believe the OP is attempting to find the location of a string by concat'ing many strings together then using indexOf to find the location of the string. I am sure it's an invalid approach.
No, I had excluded conventional looping method in my question. @jon: yeah, benchmarking looping method vs other favored looping only. I din't take into account the complexity for calculating hay=hay.join(",");
hay=hay.join(","); too requires looping for converting it to a string ?!?. Thanks for suggestions.
@PrashanthEverlasto yes, it requires looping even then. Additionally, if you want to respond to @JonSkeet you MUST do it on his post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.