0

I'm having a little problem with finding and displaying strings from an array. Haven't done any sort of code in years so I'm kind of rusty. My problem is this:

Say I have an array like this:

var titles = ["0","Little Mouse", "1","Shaman Disciple", "2","Accomplished Shaman", "3","Shaman", "4","Little Pixie"];

(the numbers before the titles are the title id's which are used, I just use titles[i-1] to fetch it, it's pretty important they're in that sort of order!)

And that I wanted to find every string in that array that contains "little" and display its corresponding number. I came up with this, but it won't work and I've already tried reading a bunch about loops and writing over stuff, but I just can't figure it out. I came up with this script:

var x=document.getElementById("title").value;
var xs=x.toLowerCase();

    for(var i = 0; i <= titles.length; i++){
        if(xs.indexOf(titles[i].toLowerCase()) != -1){
            document.getElementById("command").innerHTML = "/title " + titles[i-1];
        } else {
            document.getElementById("command").innerHTML = "no title like that";
        }
    }

It's set to go on onkeyup="dostuff()" on a textfield (which I know can't be healthy or good code, but whatever), and it DOES work if you put in the full string, it just doesn't display all the matches in the array. I do get that I SHOULD use innerHTML += blahblah and not innerHTML = blahblah, BUT it just adds to the titles indefinitely! What the hell do I do
Sorry for the wall of text!

Deda
  • 3
  • 1
  • 1
    Do you want the title or the number as your result? Why is your array like that in the first place, really? – StackSlave Sep 22 '15 at 00:52

4 Answers4

0

You want to find the index of xs inside titles[i] but you're looking for title inside xs.

It should be titles[i].toLowerCase().indexOf(xs) != -1.

Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
0

See this fiddle for your solution Link to fiddle For loop should run less than the length of the array not equal to and string should be replaced at the end of the for loop

var titles = ["0","Little Mouse", "1","Shaman Disciple", "2","Accomplished Shaman", "3","Shaman", "4","Little Pixie"];
var x="Little";  //test string
var serchStr=''; 
var xs=x.toLowerCase();
for(var i = 0; i <titles.length; i++){

    if(titles[i].toLowerCase().indexOf(xs) >-1){

        serchStr += "/title " + titles[i-1];
    } 
}
if(serchStr=='')
    serchStr = "no title like that";
alert(serchStr);
Amit.S
  • 431
  • 2
  • 9
0

Here you go:

function findInRetardedArray(find, retardedArray){
  var r = [], f = new RegExp(find, 'i');
  for(var i=0,l=retardedArray.length; i<l; i+=2){
    if(retardedArray[i].match(f)){
      r.push(retardedArray[i]);
    }
  }
  return r;
}
// run the below on an Event instead of in the console
console.log(findInRetardedArray(document.getElementById('title').value, titles));
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

Welcome back to programming and to StackOverflow. There are a ton of resources available these days, especially if you are using JavaScript.

Most browsers now include a built-in debugger and other helpful development tools.

There are also several online resources where you can write and test code without having to worry about hosting. A popular one is JSFiddle.net.

I found several problems in your code:

  1. You loop index is incrementing the i variable one too many times.
  2. The order of the xs and titles variables in the call to indexOf were the opposite of what they needed to be.
  3. You need to ignore blank xs because it always matches.
  4. You need to accumulate the matching items somehow and output them after exiting the loop.

So after the fixes, the code looks like this:

var x=document.getElementById("title").value;
var xs=x.toLowerCase();

var found = [];
if (xs) {
    // was "i <= titles.length"
    for (var i = 0; i < titles.length; i++){
        // was "xs.indexOf(titles[i].toLowerCase())"
        if (titles[i].toLowerCase().indexOf(xs) != -1){
            // need to add items to list here
            found.push(titles[i - 1]);
        }
    }
}

// this logic needs to be outside of loop
if (found.length > 0) {
    document.getElementById("command").innerHTML = "/title " + found.join();
} else {
    document.getElementById("command").innerHTML = "no title like that";
}

And here it is on JSFiddle.net: http://jsfiddle.net/qa4x7b8t/4/

Again, welcome to StackOverflow. Please be sure to upvote and accept the useful answers.

Jack A.
  • 4,245
  • 1
  • 20
  • 34
  • Ooooh, thank you so much! What a nice and detailed explanation! :) It's neat with the array of results, I think I tried that but managed to mess it up. All in all, it seems so simple now that I look at it. – Deda Sep 22 '15 at 11:43
  • Glad it helped. Please accept and upvote the answer if it was helpful. Thanks! – Jack A. Sep 22 '15 at 11:59