Java > Warmup-1 > loneTeen (CodingBat Solution)

Problem:

We'll say that a number is "teen" if it is in the range 13..19 inclusive. Given 2 int values, return true if one or the other is teen, but not both.

loneTeen(13, 99) → true
loneTeen(21, 19) → true
loneTeen(13, 13) → false


Solution:

public boolean loneTeen(int a, int b) {
  if ((a >=13 && a <= 19) && (b < 13 || b > 19))
    return true;
  else if ((b >=13 && b <= 19) && (a < 13 || a > 19))
    return true;
  else
    return false;
}

2 comments:

  1. public boolean loneTeen(int a, int b) {
    return (a < 13 || a > 19) != (b < 13 || b > 19);
    }

    ReplyDelete
  2. public boolean loneTeen(int a, int b) {
    int count = 0;
    int[] arr = new int[3];
    arr[0] = a;
    arr[1] = b;


    for(int x = 0; x<=1; x++){
    if(arr[x]>=13 && arr[x]<=19){
    count++;
    }
    }
    return count==1;
    }

    ReplyDelete