Conditional Sum | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Conditional Sum

Find the sum of two number 1 and number 2 only if both the numbers fall in a positive integer range

4th Jan 2023, 12:11 PM
Core i9
Core i9 - avatar
3 Answers
+ 3
You can use an if-statement to check if both numbers are greater than 0
4th Jan 2023, 12:16 PM
Lisa
Lisa - avatar
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().
4th Jan 2023, 5:26 PM
JuZip
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
5th Jan 2023, 4:28 AM
Keshav Kishor Ram