Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bad order of .entries() method

I have this code and need help (question in the end).

Basically the code has two lists, both lists contain numbers, and I need to add them in order. For example: first number in list 1 plus first number in list 2. Please do not recommend other solutions, because it’s the only path I have to take, it’s a simplified version of a harder task. So doing math here is just an example to run code.

const fs = require("fs");

const pathToTheListOneFile = "./list1.txt"
const pathToTheListTwoFile = "./list2.txt"


const list1 = fs.readFileSync(pathToTheListOneFile).toString().split("\n");
const list2 = fs.readFileSync(pathToTheListTwoFile).toString().split("\n");


console.log(list1)
console.log(list2)

for (const [index1, number1] of list1.entries()) for (const [index2, number2] of list2.entries()) {

    if (number1.includes("(processed) ") && number2.includes("(processed) ")) {
        console.log(`[${index1}][${index2}] Skipping ${number1} and ${number2} as has been processed before`);
        continue;
    }

    console.log(`[${index1}] [${index2}] Adding ${number1} to ${number2}`);

    const output = number1 + number2;

    if (output.stderr && output.code !== 0) {
        throw new Error(
            `[${index1}] ${index2}] Failed to add ${number1} to the ${number2} ` +
            `last '${"(processed)" + list1[index1] && list2[index2]}'`
        )
    }

    list1[index1] = "(processed) " + list1[index1]
    list2[index2] = "(processed) " + list2[index2]

    fs.writeFileSync(pathToTheListOneFile, list1.join("\n"));
    fs.writeFileSync(pathToTheListTwoFile, list2.join("\n"));

    console.log(`[${index1}][${index2}] Adding ${number1} to ${number2} has been successful\n\n`);
}


const notDone = list1.find((i) => !i.includes("(processed)"));
if (notDone) {
    console.log("Adding didn't process, re-run")
    return;
}

console.log("Adding has been successful!")

And this is running result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ '3', '4', '5' ]
[ '2', '3', '4' ]
[0] [0] Adding 3 to 2
[0][0] Adding 3 to 2 has been successful


[0] [1] Adding 3 to 3
[0][1] Adding 3 to 3 has been successful


[0] [2] Adding 3 to 4
[0][2] Adding 3 to 4 has been successful


[1] [0] Adding 4 to (processed) 2
[1][0] Adding 4 to (processed) 2 has been successful


[1] [1] Adding 4 to (processed) 3
[1][1] Adding 4 to (processed) 3 has been successful


[1] [2] Adding 4 to (processed) 4
[1][2] Adding 4 to (processed) 4 has been successful


[2] [0] Adding 5 to (processed) (processed) 2
[2][0] Adding 5 to (processed) (processed) 2 has been successful


[2] [1] Adding 5 to (processed) (processed) 3
[2][1] Adding 5 to (processed) (processed) 3 has been successful


[2] [2] Adding 5 to (processed) (processed) 4
[2][2] Adding 5 to (processed) (processed) 4 has been successful


Adding has been successful!

So the question is: how can I make it work in order only? I mean like this:

[0] [0]
[1] [1]
[2] [2]
[3] [3]
[4] [4]
[5] [5]

and skip other unnecessary additions like

[0] [1] or [2] [1]

>Solution :

You are making a nested for-loop. What you need is a single for-loop instead

for (let i = 0; i < list1.length && i < list2.length; i++) {
    let number1 = list1[i];
    let number2 = list2[i];
    //do your stuff...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading