0

I have two arrays villanStrength = [112,243,512,343,90,478] and playerStrength = [5,789,234,400,452,150] of same length, I am comparing each value of array playerStrength with villanStrength and forming up an another array which will store the either 0 or 1 (false or true) based on comparison, but the output array I am getting is not desirable. Please help me...

my code:

process.stdin.resume();
process.stdin.setEncoding('ascii');

var userInput =        //providing this externally from the file
1
6
112 243 512 343 90 478
5 789 234 400 452 150;

var testCases = "";
var numberOfPlayers = "";
var villanStrength = [];
var playerStrength = [];

process.stdin.on('data', (data) => {
    userInput = data;
    // console.log("user input = " + userInput);
    let res = userInput.split("\n");

    testCases = res[0];
    // for (i=1; i<=testCases; i++) {
        numberOfPlayers = res[1];
        // console.log("cases = " + testCases);
        // console.log("number of players = " + numberOfPlayers);

        villanStrength = res[2].split(" ");
        playerStrength = res[3].split(" ");
        console.log("villan Strength = " + villanStrength);
        console.log("player Strength = " + playerStrength);

        let isSmall = false;
        let comparisonResult = [];
        for (let j=0; j<villanStrength.length; j++) {
            for (let k=0; k<playerStrength.length; k++) {
                if (playerStrength[j] < villanStrength[k]) {
                    comparisonResult[k] = 1;                   //true = 1, false = 0
                } else {
                    comparisonResult[k] = 0;
                }
            }
            console.log("comparison result for " + j +":" + comparisonResult);
            if(comparisonResult.find((findOne) => {return findOne = 1;} )) {
                isSmall = true;
                console.log("LOSE");
                break;
            }
        }

        if (isSmall === false) {
            console.log("Win");
        }

    // }
});

The output array is comparisonResult[] and the values inside comparisonResult I am getting is as below:

villan Strength = 112,243,512,343,90,478
player Strength = 5,789,234,400,452,150
comparison result for 0: 0,0,1,0,1,0           //this should be 1,1,1,1,1,1
comparison result for 1: 0,0,0,0,1,0
comparison result for 2: 0,1,1,1,1,1
comparison result for 3: 0,0,1,0,1,1
comparison result for 4: 0,0,1,0,1,1
comparison result for 5: 0,1,1,1,1,1

in the above result it is expected that the 'comparison result for 0' should be [1,1,1,1,1,1] but it is [0,0,1,0,1,0].

12
  • Each iteration through the j loop will overwrite the previous iterations values in the comparisonResult array so comparisonResult will contain only values from the last iteration of the j loop. I don't know what you're trying to accomplish with that comparisonResult array. To capture every value of every comparison you do, that array would have to contain villanStrength.length * playerStrength.length elements, not just playerStrength.length elements since you're comparing every element in villanStrength to every element in playerStrength. Commented Feb 1, 2021 at 6:12
  • You're looping j for villanStrength and k for playerStrength, then you get [j] from playerStrength and [k] from villanStrength... That's why a meaningful variable name is useful Commented Feb 1, 2021 at 6:12
  • @jfriend00 Yes it will overwrite, actually there is some more code below but I want to check the values stored inside the 'comparisonResult' array after every iteration but here the values stored are not correct as far as I can see. Commented Feb 1, 2021 at 6:16
  • Well, you're only going to end up with a mix of results because only if playerStrength[j] < villanStrength[k], then you overwrite the previous value, otherwise, you keep the previous value. Really not sure what you're trying to accomplish at all. Commented Feb 1, 2021 at 6:17
  • 1
    Your code is comparing strings. Commented Feb 1, 2021 at 6:51

3 Answers 3

1

There are a couple problems with this code.

  1. When you compare values in your arrays, you are comparing strings, not numbers. The values you get from stdin are text values, not numeric values. So, for example '5' > '100'. I presume this is the major source of your issue. If you want to do numeric comparisons, you need to convert the strings to numbers.

  2. You are assuming that you get ALL your data on the first data event. While that may usually be true, it is not guaranteed and you should not rely on it when programming. You have to collect data in one or more data events until you have a full chunk of data you can process.

If you add these two log statements that show the actual contents of the array (not the .toString() conversion of the array):

console.log("villan strength: ", villanStrength);
console.log("player strength: ", playerStrength);

You will see this output:

villan strength:  [ '112', '243', '512', '343', '90', '478\r' ]
player strength:  [ '5', '789', '234', '400', '452', '150;\r' ]

Note, these are strings and when coming from my text file, there's a trailing \r too.

If you change this:

villanStrength = res[2].split(" ");
playerStrength = res[3].split(" ");

to this:

villanStrength = res[2].split(" ").map(item => parseInt(item.trim(), 10));
playerStrength = res[3].split(" ").map(item => parseInt(item.trim(), 10));

Then, it will trim off the newline and convert them to numbers and your comparisons will make sense. This is why the code you posted originally in your question did not generate the wrong output because you hacked in an array of numbers (for purposes of the original question), but your original code was ending up with an array of strings.

Sign up to request clarification or add additional context in comments.

Comments

1

Based on your requirement, the playerStrength loop needs to be the outer loop and comparisonResult should be an array of arrays

           villanStrength = [112,243,512,343,90,478] 
            playerStrength = [5,789,234,400,452,150]
            // ...
            let comparisonResult = [];                    //output array
            for (let j=0; j< playerStrength.length; j++) {
                 comparisonResult[j] = [];
                for (let k=0; k<villanStrength.length; k++) {
                    if (playerStrength[j] < villanStrength[k]) {
                        comparisonResult[j].push(1);                   //true = 1, false = 0
                    } else {
                        comparisonResult[j].push(0);    
                    }
                }
                console.log("comparison result for player " + j +":" + comparisonResult[j]);
             }

1 Comment

I have made some changes in the question, please tell me why I am getting that values in output array.
1

If I understand the question right you need to compare each value from array1 to array2 and generate a new array that show diffrent...

All you need is just one loop that can take both values and push result of comparison to another array

function compare() {
    const villanStrength = [112, 243, 512, 343, 90, 478];
    const playerStrength = [5, 789, 234, 400, 452, 150];
    const result = [];

    for (let i = 0; i < villanStrength.length; i++) {
        const vVal = villanStrength[i];
        const pVal = playerStrength[i];

        if (pVal < vVal) {
            result.push(1);
            continue;
        }

        result.push(0);
    }

    console.log(result);
}

My suggestion for is to separate codes to smaller funtions so you can focus on each section

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.