Match 5 Loop Set " for "

Asked

Viewed 84 times

0

I’m creating a unique id for each id, which works like this: the last 2 digits validate the previous 9 with an algorithm.

I am creating a function that finds the first 5 digits, as last 6 data. in all possible combinations!

The code works, but not as it should... I made 5 loops for , as below:

function executar(v){ // função principal
        logg("Inicio da Operacao...");
         for(var x=get('x');x<10;x++){
           for(var y=get('y');y<10;y++){
             for(var z=get('z');z<10;z++){
              for(var k=get('k');k<10;k++){
                for(var w=get('w');w<10;w++){
                 var clc = ""+x+""+y+""+z+""+k+""+w+""+v;
                 if(validarID(clc)){
                  logg(clc+": é uma possibilidade válida!");
                  pause(x,y,z,k,w);
                  return true;
                 }
                 logg(clc+": não e válido.");
               }
              }
             }
            }
           }
         logg("Fim da Operacao.");
         return false;
    }

function pause(x,y,z,k,w){
   set('x',x);
   set('y',y);
   set('z',z);
   set('k',k);
   set('w',parseInt(w)+1);
}
function set(id, valor){//guarda variáveis pra continuar depois
   document.getElementById(id).value=valor;
}
function get(id){//recupera valores apos pause
   return parseInt(document.getElementById(id).value);
}
function logg(msg){
      var obj = document.getElementById('log');
          obj.value = obj.value + "\n";
          obj.value = obj.value +  ">> " +msg;
          setTimeout(function(){
                obj.scrollTop = obj.scrollHeight;
            }, 500);
}

function validarID(id) { 
    // 
}

Exit:

// - - - - Exemplo de saída - - - //
// - - - - Dado: (833703): - - - - //
// - - - - Tinha colocado hora min seg etc mas não e importante - - - //


10s075ms>> Inicio da Operacao...
10s076ms>> 00001833703: failed.
10s076ms>> 00002833703: failed.
10s076ms>> 00003833703: failed.
10s076ms>> 00004833703: failed.
10s076ms>> 00005833703: failed.
10s076ms>> 00006833703: failed.
10s077ms>> 00007833703: failed.
10s077ms>> 00008833703: failed.
10s077ms>> 00009833703: failed.
10s077ms>> 00011833703: failed.
10s077ms>> 00012833703: failed.
10s077ms>> 00013833703: failed.
10s077ms>> 00014833703: failed.
10s078ms>> 00015833703: failed.
10s078ms>> 00016833703: failed.
10s078ms>> 00017833703: failed.
10s078ms>> 00018833703: failed.
10s078ms>> 00019833703: failed.
10s078ms>> 00021833703: failed.
10s079ms>> 00022833703: failed.
10s079ms>> 00023833703: failed.
10s079ms>> 00024833703: failed.
10s079ms>> 00025833703: failed.
10s079ms>> 00026833703: failed.
10s079ms>> 00027833703: failed.
10s079ms>> 00028833703: failed.
10s079ms>> 00029833703: failed.
10s079ms>> 00031833703: failed.
10s080ms>> 00032833703: failed.
10s080ms>> 00033833703: failed.
10s080ms>> 00034833703: failed.
10s080ms>> 00035833703: failed.
10s080ms>> 00036833703: is correct!
11s622ms>> Inicio da Operacao...
11s622ms>> 00037833703: failed.
11s623ms>> 00038833703: failed.
11s623ms>> 00039833703: failed.
11s624ms>> 00047833703: failed.
11s624ms>> 00048833703: failed.
11s624ms>> 00049833703: failed.
11s624ms>> 00057833703: failed.
11s625ms>> 00058833703: failed.
11s625ms>> 00059833703: failed.
11s625ms>> 00067833703: failed.
11s625ms>> 00068833703: failed.
11s625ms>> 00069833703: failed.
11s626ms>> 00077833703: failed.
11s626ms>> 00078833703: failed.
11s626ms>> 00079833703: failed.
11s626ms>> 00087833703: failed.
11s627ms>> 00088833703: failed.
11s627ms>> 00089833703: failed.
11s627ms>> 00097833703: failed.
11s627ms>> 00098833703: failed.
11s627ms>> 00099833703: failed.
11s628ms>> 00137833703: failed.
11s628ms>> 00138833703: failed.
11s628ms>> 00139833703: failed.
11s628ms>> 00147833703: failed.
11s629ms>> 00148833703: failed.
11s629ms>> 00149833703: failed.
11s629ms>> 00157833703: failed.
11s629ms>> 00158833703: failed.
11s630ms>> 00159833703: failed.
11s630ms>> 00167833703: failed.
11s630ms>> 00168833703: failed.
11s630ms>> 00169833703: failed.
11s631ms>> 00177833703: failed.
11s631ms>> 00178833703: failed.
11s632ms>> 00179833703: failed.
11s632ms>> 00187833703: failed.
11s632ms>> 00188833703: failed.
11s632ms>> 00189833703: failed.
11s633ms>> 00197833703: failed.
11s633ms>> 00198833703: failed.
11s633ms>> 00199833703: failed.
11s633ms>> 00237833703: failed.
11s634ms>> 00238833703: failed.
11s634ms>> 00239833703: failed.
11s634ms>> 00247833703: failed.
11s634ms>> 00248833703: failed.
11s635ms>> 00249833703: failed.
11s635ms>> 00257833703: failed.
11s636ms>> 00258833703: failed.
11s636ms>> 00259833703: failed.
11s636ms>> 00267833703: failed.
11s637ms>> 00268833703: failed.
11s637ms>> 00269833703: failed.
11s637ms>> 00277833703: failed.
11s637ms>> 00278833703: failed.
11s638ms>> 00279833703: failed.
11s638ms>> 00287833703: failed.
11s638ms>> 00288833703: failed.
11s638ms>> 00289833703: failed.
11s639ms>> 00297833703: failed.
11s639ms>> 00298833703: failed.
11s639ms>> 00299833703: failed.
11s639ms>> 00337833703: failed.
11s640ms>> 00338833703: failed.
11s640ms>> 00339833703: failed.
11s640ms>> 00347833703: failed.
11s641ms>> 00348833703: failed.
11s641ms>> 00349833703: failed.
11s641ms>> 00357833703: failed.
11s642ms>> 00358833703: failed.
11s642ms>> 00359833703: failed.
11s642ms>> 00367833703: failed.
11s642ms>> 00368833703: failed.
11s643ms>> 00369833703: failed.
11s643ms>> 00377833703: failed.
11s643ms>> 00378833703: failed.
11s644ms>> 00379833703: failed.
11s644ms>> 00387833703: failed.
11s644ms>> 00388833703: failed.
11s644ms>> 00389833703: is correct!
12s544ms>> Inicio da Operacao...
12s550ms>> Fim da Operacao.

Updated: Adjusting the value typing (str / int) and omitting pause function worked. I wanted to pause and resume function, but the important thing that what I wanted worked.

  • What, and how should it work? Isn’t it generating all the combinations? Or the validation doesn’t work?

  • It is generating less than 10 combinations and then falling into the "end of the operation", but as tests I am doing on nail the possibilities , and there are many more possibilities.

  • You know that algorithms for purposes like this are not based solely on a set of ifs, elses ou loops right ? What you need is a good equation and some logic to make it work. Otherwise, there are already own means to do what you want.

  • 1

    Do you really need five loops? I couldn’t see reason to stop using a single, multi-digit number instead of several of a digit (remembering that you can work individually with the digits inside the loop, anyway).

  • Using nested for loop with 2 levels is already kind of inefficient, imagine with 5. I think no one uses more than 2 levels like this. It confuses a lot. There must be a smart and efficient way to do this. How about using variables and trying to use only one loop? Think of an algorithm there before you go to the code.

  • I’m still gonna keep working on the code to see if I can get it to pause. This is important to not lock your Javascript in your browser, after all are 10 5 = 100,000 possibilities. In the above example input gave 1653 valid possibilities.

  • This amount of loops does not seem to be a good solution, imagine a scenario where you need to test a gigantic amount, the waiting time will probably pack the browser!

  • Incredible as it may seem, I still can’t understand the purpose of this code - compare and find, and then ? - and so it becomes difficult to try to do whatever it is, but for now I say that you are probably constructing logic poorly, and you better than anyone know for sure what the code should do. If possible formulate the problem better or make a clear summary of it.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.