1

I have got an array that consists of strings. I have made a function that searches the array based on the search term parameter. However, when i run the code it only ever outputs the string at index 0 of the array. I want it to return the corresponding url in the array when a search is run.

Any help would be very much appreciated. Thanks in advance.

4 Answers 4

1

So you are trying to return URL based on the String after the ~?

Do the line

arrayOfURL[i].toLowerCase().split('~')[i];

seem weird to you? Imagine as i increases, eg. i = 4

 arrayOfURL[4].toLowerCase().split('~')[4];

Does that last [4] make sense? I am guessing the reason it never got past the first element is because the code actually erroring out on that part.

I think what you want is (likewise for the return line, you'll want [0]

 arrayOfURL[i].toLowerCase().split('~')[1];

I would also take a look at

if (z >= searchtoLower) 

what are you trying to compare there?

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

2 Comments

Yes, i'm trying to return the url based on what is in the search parameter when the function is called. I'm not sure why [4], surely there is no index of 4 in the array? It still doesnt seem to be outputting the right url? Any other tips? thank you for your time!
[4] was an example to illustrate how the code will break down. When you want to get the search term (the 2nd part of the String) you'll need [1] When you want to return the url (the first part of the String) you'll need [0]
1

The problem may be in the second i param:

var z = arrayOfURL[i].toLowerCase().split('~')[i];  

The string will be splitted into 2 parts (index 0, 1). Why did you select part i?

Comments

0

This is a correct version of your program:

var arrayOfURL = [
                 "http://www.google.co.uk~Google is a search engine.",
                 "http://www.yahoo.co.uk~Yahoo is another search engine.",
                 "http://bing.com~Bing is a decision engine."
                 ];

function findURL(arrayOfURL,search)
{   
    var searchtoLower = search.toLowerCase();
    for (var i = 0; i < arrayOfURL.length; i++)
    {     
        var z = arrayOfURL[i].toLowerCase().split('~')[1]; 
        if (z.indexOf(searchtoLower) != -1)
            return arrayOfURL[i];
    }

     return "Nothing Found!";
}


findURL(arrayOfURL,"decision")

I hope it can help you.

Comments

0

I think you should be doing

var terms = arrayOfURL[i].toLowerCase().split('~');

if(0 <= terms[1].indexOf(searchToLower))
           // ^    ^ 
           // |    |-- 0 <= indexOf method determines
           // |        if searchToLower is a substring of terms[1] 
           // |               
           // |-- term[1] gets the part after the first "~"

and

return terms[0]; //terms[0] is the part before the first "~"

I would also consider returning null or the empty string "" in case of failure (instead of returning the arbritrary "Nothing Found!" message)

1 Comment

Hey thanks, but it still doesnt seem to returning the correct string from the array?

Your Answer

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