gudii9
asked on
dateFashion java challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p103360
i wrote as below
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
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;
}
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
public int dateFashion(int you, int date) {
return you <= 2 || date <= 2 ? 0 : you >= 8 || date >= 8 ? 2 : 1;
}
Jim
@Jim, yes it's a one liner but which is easier to comprehend? ;)
ASKER
public int dateFashion(int you, int date) {
if(you<=2 ||date<=2){
return 0;
}
if(you>=8 || date>=8){
return 2;
}
return 1;
}
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;
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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;
}
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 ?
ASKER
Open in new window
below passed all.How can i improve. please advise