-1

I want to create a function to get absolute id of fractal subobject by layer/row and column id. In an fractal object there are repeating subobjects, you can split them into layers. Any object with identifier "W" contains 7x G's, each object identified by G's again contain 7x N's etc.

if layer is ...
...3 (rowId=2) then only strings with "N"...
...2 (rowId=1) then only strings with "G"...
...
are counted.

The raw code:

const rawData = [["W1","W2","W3","W4","W5","W6","W7"],
  ["G1","G2","G3","G4","G5","G6","G7"],
  ["N1","N2","N3","N4","N5","N6","N7"],
  ["T1","T2","T3","T4","T5","T6","T7"]]
  
const selIds=[0,1,2,4] // selected indices of W1 G2 N3 T5 (range per array element: [0-6])

// to calculate absolute fractal "cell" counter; following input is given by click on subobject
rowId=1 // layer index - 1 (0-6)
colId=2 // element index - 1 (0-6)

I think result can be calculated this way?

result=((7^1*selIds[0])+selIds[1]*7^0)*7^(rowId+1)+3=(0+1)*49+3=52

On click the selIds change with given rowId & colIds to selIds=[0,2,2,4]; letters of rawData are just arbitrary, because in reality they are like identifiers of planets, solar systems, galaxies and so on

My functions so far:

const getCurrentIndex = function(elementIndex, layerIndex) {
    let currentIndex = 0
    if(layerIndex > 0) {
        for(let j = 0; j < layerIndex; j++) {
            currentIndex += (selIds[j]) * Math.pow(7, layerIndex-j)
        }
    }

    currentIndex += elementIndex
    currentIndex += 1
    return currentIndex
}

Examples (layer/row & element/col = absolute id):

W1:G1:N1:T1 = 1
W1:G1:N1 = 1
W4:G5:N6 = 3*7*7 + 4*7 + 6 = 181
W2:G2:N3 = 1*7*7 + 1*7 + 3 = 59
W3:G4:N3 = 2*7*7 + 3*7 + 3 = 122
W4:G4:N4 = 3*7*7 + 3*7 + 4 = 172
W2:G2:N3:T4 = 1*7*7*7 + 1*7*7 + 2*7 + 4 = 410

Column id is the number of the last string element split by :.

7
  • @Barmar This comment thread is kinda funny to read. I assume there's another side to this that was deleted? Commented Sep 24, 2024 at 22:06
  • This looks like a Base-7 to Base-10 conversion, with an "additional rule" that makes no sense to me (at least for now). E.g. W1:G1:N1:T1 becomes 1111 (b7) at first. The "additional rule" says "decrease all digits by 1 except the last digit", that gives 0001 (b7) which is one in Base-10. All other combinations work in the same way. Is my interpretation correct? Commented Sep 24, 2024 at 22:15
  • @ITgoldman Maybe the selIds can be used in similar logic as I just described, but now [0,2,2,4] becomes 0225 (b7) because now the last index vanuit has to be increased by 1. Commented Sep 24, 2024 at 22:25
  • what is the name of the function you want to accomplish and what are the parameters? Commented Sep 24, 2024 at 22:47
  • Once you have constructed the Base-7 string, the conversion to a regular int (Base-10) value is very easy: parseInt(base7Str, 7). Commented Sep 24, 2024 at 22:48

1 Answer 1

1

The solution is:

const getCurrentIndex = function(colId, rowId) {
    let currentIndex = 0
    for(let j = 0; j < rowId; j++) {
        currentIndex += (selIds[j]) * Math.pow(7, rowId-j)
    }

    currentIndex += colId
    currentIndex += 1
    return currentIndex
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.