+ 1
Conditional Sum
Find the sum of two number 1 and number 2 only if both the numbers fall in a positive integer range
3 Answers
+ 3
You can use an if-statement to check if both numbers are greater than 0
0
Hey Medo,
You could check like Lisa says with if for > 0.
And if I understand you in the right way and it's supposed to be integer exclusive and not e.g. positive floats. You could catch it with taking the input inside the int()-method or later by checking with type().
0
Here is a function to find the sum of two numbers only if both the numbers are >0, else it'll return undefined.
function sumOfPositive(a,b){
return a>0 && b>0 ? a+b : undefined
}
console.log(sumOfPositive(2,5)); // 7
console.log(sumOfPositive(-2,5)); // undefined
console.log(sumOfPositive(0,0)); // undefined