Each condition test must be self-sufficient and complete. While char == "A"
qualifies, "a"
does not meet the criteria. The incorporation of logical operators such as &&
and ||
does not alter this rule.
Furthermore, when returning a value, do so without assignment.
The corrected code should resemble:
function get (char) {
if (char == "A"|| char == "a") {
return 5;
} else if (char == "B"|| char == "b") {
return 4;
} else if (char == "C"|| char == "c") {
return 3;
} else if (char == "D"|| char == "d") {
return 2;
} else if (char == "F"|| char == "f") {
return 0;
}
}
console.log(get("A"));
console.log(get("b"));
console.log(get("C"));
console.log(get("d"));
console.log(get("f"));
To simplify the process, it is recommended to convert the input to either uppercase or lowercase before testing. This way, you only need to assess the string and not the case sensitivity:
function get (char) {
char = char.toLowerCase();
if (char == "a") {
return 5;
} else if (char == "b") {
return 4;
} else if (char == "c") {
return 3;
} else if (char == "d") {
return 2;
} else if (char == "f") {
return 0;
}
}
console.log(get("A"));
console.log(get("b"));
console.log(get("C"));
console.log(get("d"));
console.log(get("f"));
Alternatively, for a more concise approach that eliminates the need for if/then
statements altogether, you can directly return the index position of the match in an array.
let scores = ["f","","d","c","b","a"];
function get (char) {
return scores.indexOf(char.toLowerCase());
}
console.log(get("A"));
console.log(get("b"));
console.log(get("C"));
console.log(get("d"));
console.log(get("f"));