Each test in a condition must be a complete test that can stand on its own. char == "A"
is, but "a"
isn't. The use of a logical operator such as &&
and ||
doesn't change that.
Also, when you return, you only return a value, not an assignment.
The code must be:
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"));
But, to make things simpler, it's customary to convert the input to either upper or lower case before testing and then you only have to test for the string and not the case:
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"));
Or, if you are looking for a more compact way of getting this result that removes the need for if/then
in the first place, you can simply 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"));