[英]Getting wrong elements in an array after comparing two array values
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.我有两个长度相同的 arrays
villanStrength = [112,243,512,343,90,478]
和 playerStrength = [ playerStrength = [5,789,234,400,452,150]
,我将数组playerStrength
的每个值与villanStrength
的每个值进行比较并形成另一个数组 1(假或真)将存储要么基于比较,但我得到的 output 阵列是不可取的。 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: output 数组是 compareResult[] ,我得到的 compareResult 中的值如下:
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].在上述结果中,预计“0 的比较结果”应该是 [1,1,1,1,1,1] 但它是 [0,0,1,0,1,0]。
Based on your requirement, the playerStrength loop needs to be the outer loop and comparisonResult should be an array of arrays根据您的要求, playerStrength 循环需要是外循环, compareResult 应该是 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]); }
There are a couple problems with this code.这段代码有几个问题。
When you compare values in your arrays, you are comparing strings, not numbers .当您比较 arrays 中的值时,您是在比较字符串,而不是数字。 The values you get from stdin are text values, not numeric values.
您从标准输入获得的值是文本值,而不是数值。 So, for example
'5' > '100'
.因此,例如
'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.
如果要进行数字比较,则需要将字符串转换为数字。
You are assuming that you get ALL your data on the first data
event.您假设您在第一个
data
事件中获得了所有数据。 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.您必须在一个或多个
data
事件中收集数据,直到您拥有可以处理的完整数据块。
If you add these two log statements that show the actual contents of the array (not the .toString()
conversion of the array):如果添加这两个显示数组实际内容的日志语句(不是数组的
.toString()
转换):
console.log("villan strength: ", villanStrength);
console.log("player strength: ", playerStrength);
You will see this output:你会看到这个 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.请注意,这些是字符串,当来自我的文本文件时,也有一个尾随
\r
。
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.
这就是为什么您最初在问题中发布的代码没有生成错误的 output 因为您破解了一个数字数组(出于原始问题的目的),但您的原始代码最终得到了一个字符串数组。
If I understand the question right you need to compare each value from array1 to array2 and generate a new array that show diffrent...如果我理解正确的问题,您需要比较从 array1 到 array2 的每个值并生成一个显示不同的新数组...
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我的建议是将代码分成较小的功能,以便您可以专注于每个部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.