Issue
This is the code that Im working on
testCheckNull(value, student) {
// Check for college applicants
if (value == "college") {
if (student.grade != null) {
return value + " ready"
} else if (student.sat != null) {
return value + " not ready"
}
}
// Check for the rest of applicants
if (student.grade != null) {
return value + " ready (non-college)"
} else if (student.sat != null) {
return value + " not ready (non-college)"
}
}
So if you notice the nested and last set of if condition are really just repeating itself with the difference to see if the applicant is for college, Im trying to lean out the code and avoid redundancy -- is there a way to simplify this?
Solution
Sure — extract the repeating part, and parametrise the difference:
const nonCollegeParenthetical = value == "college" ? "" : " (non-college)";
if (student.grade != null) {
return value + " ready" + nonCollegeParenthetical;
} else if (student.sat != null) {
return value + " not ready" + nonCollegeParenthetical;
}
Answered By - Amadan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.