Problem:
The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean isSummer, return true if the squirrels play and false otherwise.
squirrelPlay(70, false) → true
squirrelPlay(95, false) → false
squirrelPlay(95, true) → true
Solution:
public boolean squirrelPlay(int temp, boolean isSummer) { if (isSummer && temp <= 100 && temp >= 60) return true; if (!(isSummer) && temp <= 90 && temp >=60) return true; else return false; }
Bad solution.
ReplyDeletesee that, you have used temp>=60 twice and in both cases it is returning true.
Try differently.
public boolean squirrelPlay(int temp, boolean isSummer) {
ReplyDeletereturn ((temp >= 60 && temp <= 90) || (isSummer && temp >= 60 && temp <= 100));
}
public boolean squirrelPlay(int temp, boolean isSummer) {
ReplyDeletereturn (!isSummer && 60 <= temp && temp <= 90) || (isSummer && 60<= temp && temp <= 100);
}
public boolean squirrelPlay(int temp, boolean isSummer) {
ReplyDeletereturn (isSummer? temp>=60 && temp <=100 : temp>=60 && temp <=90 );
}
public boolean squirrelPlay(int temp, boolean isSummer) {
ReplyDeletereturn 60 <= temp && (temp <= 90 || temp <= 100 && isSummer);
}
public boolean squirrelPlay(int temp, boolean isSummer) {
ReplyDeleteif((isSummer) && (temp >= 60 && temp <= 100))
return true;
else if (!(isSummer) && (temp >= 60 && temp <= 90))
return true;
else
return false;
}
public boolean squirrelPlay(int temp, boolean isSummer) {
ReplyDeleteif(temp>=60&&((temp<=90&&!isSummer)||(temp<=100 && isSummer))){
return true;
}else{
return false;
}
}
In JavaScript:
ReplyDeletefunction squirrelPlay(temp, isSummer){
if((temp >= 60 && temp <= 100 && isSummer) || ((temp >= 60 && temp <= 90) && !isSummer)) {
return true
}
return false
}