'ws.write = (result.join(',') + '\n'); && TypeError: result.join is not a function ...how i solve this type error
help to solve in this javascript problem. Give me clear documentation about (join).
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const a = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10));
const b = readLine().replace(/\s+$/g, '').split(' ').map(bTemp => parseInt(bTemp, 10));
const result = compareTriplets(a, b);
ws.write = (result.join(',') + '\n');
ws.end();
}
Solution 1:[1]
Clear documentation for join
const result = compareTriplets(a, b);
Not sure what compareTriplets is but based on the word compare I am assuming it returns a boolean. You are trying to join a boolean expression. If you want one string containing of A and B then put A and B into an array and then use join. But with so little information it is hard to understand what you are trying to accomplish.
Based on your code I am assuming A and B both are arrays. If you want to join the elements together do this. Also assuming result is a boolean.
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const a = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10));
const b = readLine().replace(/\s+$/g, '').split(' ').map(bTemp => parseInt(bTemp, 10));
const result = compareTriplets(a, b);
if(result){
ws.write = (a.join(',') + ',' + b.join(',') + '\n');
}
ws.end();
}
Solution 2:[2]
I have gone through the same issue in a problem on Hackkerank,what i was doing wrong was that i was returning results as "return(scoreOfA , scoreOfB)" but the correct format was "return([scoreOfA,scoreOfB])" as it is using "join" which takes an array as an input .
function compareTriplets(a, b) {
let scoreA=0,scoreB=0;
for(let i=0;i<3;i++){
if(a[i] > b[i]){ scoreA += 1}
else if(a[i]< b[i]){ scoreB += 1}
}
return ([scoreA,scoreB])
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Aditi Priya |
