How to cross reference arrays and get the index when using an uploaded text file?

73 Views Asked by At

I am working on something that will import a text file with a sequence of numbers, separate the numbers from the strings and save to two separate arrays, then compare the numbers array to a third array that holds the triangular sequence of numbers in it. Once the numbers array matches with a number from the triangular sequence, the out put will be an index number of inside the number array. After, using the index number, will reference it to the text and pull the index numbers of that string array.

A visual explanation of what the process I want:

visual example of what i am going for

I am stuck on how to move to each 'step' of the process. How can I compare the sequence to the numArray and how do I get a reusable output of the index that I can later use to resort the text array? The key is that the data is coming from a text file, being split into an array (numArray), and then being indexed. I keep get undefined and [-1].

I'm almost certain this is easier to do with python or another language, if this isn't possible with vanilla js, then I honestly learn something that's more suitable.

Thank you for any help. I am very new to all this.

Below is the code I have so far.

EDIT: i changeded ap[0] to ap[n] which saves the sequence to an array.

    for (let n=1; n <= 300; n++) {
        //create an array to store our triangular sequence
        let ap =[] 

        //generates our triangular sequence
         ap[n] = n * (n + 1) / 2 //generates our triangular sequence

       //cycles through our array of numbers and should compare and output the value
       for (let i=0; i <= arrayNum.length; i++) {
        arrayNum[0] = i
        if (arrayNum[i] == ap[n]){
            results = ap.map(i=>arrayNum.indexOf(i))
            console.log(results)
            
        
             }
         }
    }
}
0

There are 0 best solutions below