Search code examples
javascriptadobe-illustrator

an algorithm to store '{' and '}' in two arrays in javascript


I am trying to make an algorithm. The inputs may be like the following.

- a{1}b{1}c{1}
- a{a{1}b{1}c{1}}
- a{a{a{1}b{1}}b{b{1}c{1}}d{1}}

And I am having two arrays named as "ys" and "ns". I need to store the starting braces '{' in the array "ys" and ending braces '}' in the array "ns". But the challenge is to store the position of the starting and ending braces in the input string to be same corresponding position in the two arrays. For example, consider the 2nd input. The output must be like this.

ys[0]=>1, ys[1]=>3, ys[2]=>7, ys[3]=>11.
ns[0]=>14, ns[1]=>5, ns[2]=>9, ns[3]=>13.

My code:

var lastPoint = line.pathPoints[line.pathPoints.length - 1].anchor; // [x, y]

var eqValues = (prompt("Enter something ", "", "")); //getting the values to write
var eqValuesCopy = eqValues;


    var nFlag=0;
    for(var i=0;i<eqValuesCopy.length;i++){
        if(eqValuesCopy[i]=="{"){
            if(i!=0){
                validBCheck=eqValuesCopy[i-1]+eqValuesCopy[i];
                if(validBCheck!="/{"){
                    ys.push(i);
                }
            }
        }
        ysLength=ys.length;
    }
    for(var i=0;i<eqValuesCopy.length;i++){
        if(eqValuesCopy[i]=="{"){
            nsLength=ns.length;
            var j=i;
            while(j<eqValuesCopy.length){
                j++;
                if(nsLength<ysLength){
                    if(eqValuesCopy[j]=="}"){
                        ns[nsLength]=j;
                        break;
                    }else if(eqValuesCopy[j]=="{"){
                         nsLength++;
                    }else{

                    }
                }
            /*else{ 
                    for(var k=0;k<=ysLength;k++){
                           // alert(ns[k]);
                        if(ns[k]==undefined){
                            ns[k]=j;
                            //alert(ns[k]);
                        }
                    }
                }*/
            }
        }
    }

I am writing this program in javascript inside illustrator. Only thing is I couldn't make the logic for this algorithm.


Solution

  • This might work

        for(var i=0;i<eqValuesCopy.length;i++){
            if(eqValuesCopy[i]=="{"){
                if(i!=0){
                    validBCheck=eqValuesCopy[i-1]+eqValuesCopy[i];
                    if(validBCheck!="/{"){
                        ys.push(i);
                    }
                }
            }
            ysLength=ys.length;
        }
        for(var i=0;i<eqValuesCopy.length;i++){
            nsLength=ns.length;
            if(nsLength<ysLength){
                if(eqValuesCopy[i]=="{"){
                    var j=i;
                    while(j<eqValuesCopy.length){
                        j++;
                        if(eqValuesCopy[j]=="}"){
                            ns[nsLength]=j;
                            break; 
                        }else if(eqValuesCopy[j]=="{"){ 
                            nsLength++;
                        }else{ 
    
                        }
                    }  
                    i=j;
                }
            }
        }
        for(var k=0;k<eqValuesCopy.length;k++){
            if(eqValuesCopy[k]=="}"){
                var h=k;
                while(h<eqValuesCopy.length){
                    h++;
                    if(eqValuesCopy[h]=="{"){
                        break;
                    }else if(eqValuesCopy[h]=="}"){ 
                        nsDummy.push(h);
                        break;
                    }else{ 
    
                    }
                }
            }
        }
    nsDummy.reverse();
    for(var x=0;x<nsLength;x++){
        if(ns[x]===undefined){
            ns[x]=nsDummy[x];
        }
    }