Problem:
Given 2 ints, a and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20.
sortaSum(3, 4) → 7
sortaSum(9, 4) → 20
sortaSum(10, 11) → 21
Solution:
public int sortaSum(int a, int b) { int sum = a+b; if(sum >= 10 && sum <= 19) return 20; else return sum; }
public int sortaSum(int a, int b) {
ReplyDeleteint sum = a + b;
return sum >= 10 && sum <= 19 ? 20 : sum;
}
public int sortaSum(int a, int b) {
ReplyDeleteint sum = a+b;
boolean inRange = sum >= 10 && sum <= 19;
return !inRange ? sum:20;
}
public int sortaSum(int a, int b) {
ReplyDeletereturn ( 10 <= (a + b) && (a + b) <= 19)? 20 : (a + b);
}
In JavaScript:
ReplyDeletefunction sortaSum(a, b) {
if(a + b >= 10 && a + b <= 20) {
return 20
} else {
return a + b
}
}
public int sortaSum(int a, int b)
ReplyDelete{
int sum;
sum=a+b;
if(sum>=10 && sum<=19)
{
return 20;
}
return sum;
}