I want to build on @Abion47's answer, because it hits on a lot of key points but misses a few key pointstwo crucial items.
- Your program should not just print successes; it should also print descriptive error messages. Returning a blank screen if someone inputs "10, 30" makes them assume the application is broken, not that it's behaving as intended.
- Returning a triple is based on your function name is counterintuitive. The function calculateSumDiff should return the sum and the difference of two numbers. If you want to later decide negative numbers are a no-go, you should do that in the business logic at that point in time. As another dev, if I pass 25, 50 into the function, I'd expect the answer to be {75, -25}, not {75, -25, invalid}. If I wanted to check if either of them is negative, I'd check that both the sum and difference are >0, I would never think to check if calcuateSumDiff came back as "invalid"
To solve these two problems, I'd suggest changing Abion's answer to this:
function setup() {
createCanvas(windowWidth, windowHeight);
background('#004567');
noStroke();
fill('white');
textSize(30);
// I assume eventually you'll be getting this input from a user, so I pulled it out of the function call
const input1 = 55;
const input2 = 33;
const myDouble = calculateSumDiff(input1, input2);
if (myDouble.sum > 0 && myDouble.difference > 0) {
text(myDouble.sum, 100, 200);
text(myDouble.difference, 100, 300);
} else {
text(`${input1}, ${input2} did not create a positive outcome for both the sum and the difference`, 100, 200);
}
}
/**
* Returns the sum and difference of two numbers passed to the function.
* @param operand1 {number}
* @param operand2 {number}
* @returns {{sum: number, difference: number}}
*/
function calculateSumDiff(operand1, operand2) {
return calcResults = {
sum: operand1 + operand2,
difference: operand1 - operand2
};
}