I'm just going through some hackerrank challenges and this cropped up in my function below.
The second method of return foo + " " + bar gave me an error, but the first method worked. Why? What are the differences between the two methods of returning here?
Edit:- Full source for the interests of a commenter...
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.trim().split('\n').map(str => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the solve function below.
*/
function solve(a0, a1, a2, b0, b1, b2) {
/*
* Write your code here.
*/
let aScore = 0;
let bScore = 0;
function compare(valA, valB){
if (valA < valB){
bScore += 1;
}
else if (valA == valB){
}
else {
aScore += 1;
}
}
compare(a0, b0);
compare(a1, b1);
compare(a2, b2);
return [aScore, bScore];
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const a0A1A2 = readLine().split(' ');
const a0 = parseInt(a0A1A2[0], 10);
const a1 = parseInt(a0A1A2[1], 10);
const a2 = parseInt(a0A1A2[2], 10);
const b0B1B2 = readLine().split(' ');
const b0 = parseInt(b0B1B2[0], 10);
const b1 = parseInt(b0B1B2[1], 10);
const b2 = parseInt(b0B1B2[2], 10);
let result = solve(a0, a1, a2, b0, b1, b2);
ws.write(result.join(" ") + "\n");
ws.end();
}
With return [aScore, bScore]
you are returning an array with two elements while return aScore + " " + bScore
returns a single string