Javascript: matching a dynamic string against an array -
I'm trying to teach myself Javascript What I assumed was simple, but in relatively quick problems Attended
I am trying to find the string for any other string given by the user.
My code is so far:
var source = "XREs2qqAQfjr6NZs6H5wkZdOES5mikexRkOPsj6grQiYNZfFoqXI4Nnc1iONKVrA"; Var searchString = []; // user input searchString = prompt ("enter search string"); Var hit = []; Var one = 0; Var two = 0; Var k = 0; Var source search = function (text) {for each letter of the source (i = 0; i & lt; source.length; i ++) {// if (source [i] === search string [0]) {// If a letter in the source matches the first element in the user input one = source.indexOf (i); // Confused from here (P = Search String. Langham; P & gt; 0; p--) {}}}}; SourceSearch (searchString); I thought:
- Check to see if the first loop gets a letter that matches the first letter of the user input < / Li>
- If it matches, check to see if you match the X characters before the next X characters in the next string
- If they all match, then they Hit in the hit array
My problem: I do not have to tell if without the statement Gaddafi again how to walk with the array without the victim of a nest, and even then, it would like to work with any input that I programs to consider will not be enough, this.
Any ideas will be useful, thank you very much in advance.
Note: There are some unstable variables that I was testing, but I could not work them.
Other answers indicate as yet, in the JavaScript string a indexOf is the function you want if you want to see how this has been done "by hand", then you can modify your function like this: var sourceSearch = function (Text) {var i, j, ok; // Always declare their local variables the world is corrupt! // (i = 0; i & lt; source.length; i ++) for each start position {ok = true; // check for a match (j = searchString.length - 1; ok & amp;; j = 0; -j) {ok = source [i + j] === search string [j]; } If (OK) {// Started from index in search string source}}}; This function searches for all the positions on the source , on which the search string was found (of course, you loop on the first success Can break out.) The argument is that in order to use the external loop, in the source , each candidate is in advance to start the situation and use the internal loop to check that The situation is actually a match condition for searchString . This is not the best algorithm for the discovery of stars The built-in algorithm is very fast (both because it is a better algorithm and because it is the original code).
Comments
Post a Comment