Home > Net >  Change position in for javascript
Change position in for javascript

Time:11-29

good morning, sorry first of all for my english. I'm trying to do a double loop to iterate through two strings, the thing is, I want the ocrString to start one position later each time, so that it can iterate through the string in order to see if there are any matches. That is, I want to find the matches without necessarily being equal in length and without being able to order it.

let ocrString = "casaidespcasa";
let pattern = "idesp";
let conteo = 0;

checkIDESP(ocrString, pattern);

function checkIDESP(ocrString, pattern) {
    let ocrStringSeparado = ocrString.split("");
    let patternSeparado = pattern.split("");

    for (i = 0; i < ocrStringSeparado.length; i  ) {
        for (x = 0; x < patternSeparado.length; x  ) {
            console.log(ocrStringSeparado[i], pattern[x]);

            if (ocrStringSeparado[i] == pattern[x]) {
                conteo  ;
            }
        }
    }

    if (conteo <= 3) {
        console.log(conteo, "No sé si es un dni");
    } else {
        console.log(conteo, "es un dni");
    }
}

Some way to go through the position of an array so that it first starts with 'Casaidespcasa' and then 'Asaidespcasa' etc.

CodePudding user response:

Well, maybe the following would work for you?

const string = "casaidespcasa";

function comp(str, pat){
 let pl=pat.length, sl=str.length, res=[];
 for (let i=0; i<=sl-pl; i  ){
   let s=str.slice(i,i pl); // get a portion of the original string
   let n=s.split("").reduce((a,c,j)=>a (c==pat[j] ? 1 : 0), 0); // count matches
   if (n>2) res.push([i,s]); // at least 3 character must match!
 }
 return res;
}

// do the string comparison with an array of patterns:
["idesp","1detp","deaspdc","cosa","asaic"].forEach(p=>console.log(p ":",comp(string,p)))

The function returns an array of possible "fuzzy" matches: Each entry is an array, containing the position and the matching substring.

CodePudding user response:

That won't answer totally to your question (I don't really understand by the way).

Now for the last part:

"Some way to go through the position of an array so that it first starts with 'Casaidespcasa' and then 'Asaidespcasa' etc."

Perhaps that can help for you to solve your problem.

let ocrString = "casaidespcasa";

let ocrStringSeparado = ocrString.split("");
decreaseArr(ocrStringSeparado);
decreaseStr(ocrString);


function decreaseArr(arr) {
  console.log(arr);
  arr.shift();

  // do something...

  if (arr.length > 0) {
    decreaseArr(arr);
  }
}

function decreaseStr(str) {
  console.log(str);
  str = str.substring(1);

  // do something...

  if (str.length > 0) {
    decreaseStr(str);
  }
}

First function is with array, second with string.

  • Related