Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

dateFashion java challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p103360
i wrote as below
public int dateFashion(int you, int date) {
int result_No=0;
int result_Yes=2;
int result_MayBe=1;
  if(you>=8 && date>=8){
  return result_Yes;
  }
  
  if(you<=2 && date<=2){
  return result_No;
  }
  
 
  return result_MayBe;
}

Open in new window



i am failing below

Expected      Run            
dateFashion(5, 10) → 2      1      X         
dateFashion(5, 2) → 0      1      X         
dateFashion(5, 5) → 1      1      OK         
dateFashion(3, 3) → 1      1      OK         
dateFashion(10, 2) → 0      1      X         
dateFashion(2, 9) → 0      1      X         
dateFashion(9, 9) → 2      2      OK         
dateFashion(10, 5) → 2      1      X         
dateFashion(2, 2) → 0      0      OK         
dateFashion(3, 7) → 1      1      OK         
dateFashion(2, 7) → 0      1      X         
dateFashion(6, 2) → 0      1      X         
other tests
X       

Please advise on how to fix and improve
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public int dateFashion(int you, int date) {
int result;

  if((you>=8 || date>=8)&& !(you<=2 || date<=2)){
  return result=2;
  }
  
  else if(you<=2 ||date<=2){
  return result=0;
  }
  
 else
 return result=1;
  
}

Open in new window

below passed all.
How can i improve. please advise
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
public int dateFashion(int you, int date) {
  return you <= 2 || date <= 2 ? 0 : you >= 8 || date >= 8 ? 2 : 1;   
}

Open in new window

Jim
@Jim, yes it's a one liner but which is easier to comprehend? ;)
Avatar of gudii9

ASKER

public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
  if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

Open in new window


can i write above code like below instead of if followed by other if can i write if followed by else if




public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
 else if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

Open in new window

Avatar of gudii9

ASKER

public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
  else if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

above also passed all.

If with if scenario the flow goes though both if related code right
whereas
if with else-if goes through either if or else-if right but never both same time which seems more efficient right?
please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

If each block wasn't returning that might be true. In this case, it doesn't matter because the return when you or date <= 2 prevents the next of front being evaluated so structuring it as if/if or if/else if doesn't impact the efficiency.

can you please elaborate with example. I did not get completely?
Avatar of gudii9

ASKER

public int dateFashion(int you, int date) {
  if(you<=2 ||date<=2){
    return 0;
  }
  else if(you>=8 || date>=8){
    return 2;
  }
  return 1;
}

Open in new window

my code is returning in each block if (0)and also else-if so(2) thats why it does not impact efficiency structuring it as if/if or if/else if ?