0

I'm working on a fairly simple web application that accepts user input in either number or word form. The basic premise behind this is I need to find the word in the array if I start with the number and/or the index number of the word if I start with the word. I have created a function that can do both. The only catch is, if I don't manually type out the list like let generic_list = ["Item1", "Item2", "Item3"];, the index isn't found when trying to load it from the text file.

The code I am using to load the text file is:

$.get("list.txt", function(data) {
    let list_loaded = data.split("\n");
})

This is the code that I'm using to find the index that works if I manually make the list.

let item_index = list_loaded.indexOf(item_index_im_searching_for);

When I manually create the list, the return value will be the proper index number. If the text file loads the list the value will return -1 undefined. I've done some reading around, but I'm not exactly sure what I'm missing. Hoping some of you JavaScript vets can lead me to the right direction.

Also, when testing in the console, both lists appear to exactly the same.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
bjd
  • 3
  • 1
  • I'm guessing that the line starting `let item_index` exists outside of the `$.get` function? – Heretic Monkey Feb 19 '20 at 22:03
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Heretic Monkey Feb 19 '20 at 22:03
  • @HereticMonkey I didn't think about that when formatting this post. Sorry about that. It is in fact the very next line after the list is made with split inside of the $.get function. I saw you posted a link. I need to go read through it, but wanted to clear that up first. – bjd Feb 19 '20 at 22:11

1 Answers1

0

Check JSFiddle for details related to javascript indexOf method: https://jsfiddle.net/ecx8Lhtj/

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
var htmlText = "<p>Finding position of the first occurrence for word welcome from string is at "+ n +"</p>";

str = str.split(" ");
htmlText += "<p>Array Elements="+ str +"</p>";
htmlText += "<p>Also check CONSOLE for seeing array elements.</p>";
console.log(str);
n = str.indexOf("welcome");
htmlText += "<p>a. Finding position of the first occurrence for word welcome from array elements is at "+ n +"</p>";

n = str.indexOf("world");
htmlText += '<p>b. Finding position of the first occurrence for word welcome from array elements is at "world" '+ n +'</p>';

n = str.indexOf("world,");
htmlText += '<p>b. Finding position of the first occurrence for word welcome from array elements is at "world," '+ n +'</p>';

document.getElementById("displayText").innerHTML = htmlText;
<p>JavaScript String indexOf() Method, reference URL: https://www.w3schools.com/jsref/jsref_indexof.asp</p>
<p><h3>Definition and Usage</h3></p>
<p>The indexOf() method returns the position of the first occurrence of a specified value in a string.</p>
<p>This method returns -1 if the value to search for never occurs.</p>
<p><b>Note</b>: The indexOf() method is case sensitive.</p>
<p>Whenever indexOf used with array elements, it will try to find searching text as a complete text against each element from an array. If such match found then only it will return an index otherwise it will return -1.</p>
<p>Please see below examples:</p>
<p id="displayText"></p>

Whenever we are using javascript indexOf method against an array, then it will try to find a searching string as an exact match against each element of an array. And if exact match found then only it will return proper index value else it will return -1.

Prasad Wargad
  • 737
  • 2
  • 7
  • 11