0

I'm trying to create a function that receives an character and returns a digit.

Here's my code:

function get (char)  {
  if (char == "A"||"a") {
    return char = 5
  } else if (char == "B"||"b") {
    return char = 4
  } else if (char == "C"||"c") {
     return char = 3
  } else if (char == "D"||"d") {
     return char = 2
  } else if (char == "F"||"f") {
     return char = 0
  }
}

but anytime I run it against any character, it always returns 5.

Any help please.

Derek Jin
  • 652
  • 2
  • 12
  • 29
samuelorobosa
  • 45
  • 1
  • 8
  • 8
    You're testing the value `"a"` which is true in a boolean context. You need to compare it to char instead: `if (char == "A"|| char == "a") {` – Nick Sep 29 '19 at 21:39
  • 1
    You should make your character lowercase (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase), and use a switch/case (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch). This will make your code more readable. – Seblor Sep 29 '19 at 21:41
  • 1
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Robin Zigmond Sep 29 '19 at 21:46
  • (The question I've flagged is about Python rather than Javascript, but the syntax differences between the languages shouldn't matter - that's still the exact same issue that is affecting this question.) – Robin Zigmond Sep 29 '19 at 21:47

4 Answers4

2

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"));
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

inspired from Scott Marcus answser
(it's just a minor "error" correction)

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

const scores = ['f','','d','c','b','a']

delete(scores[1])  // minor correction on Scott Marcus answer

const get=char=>scores.indexOf(char.toLowerCase())

console.log('A ->',get('A'));  // 5
console.log('b ->',get('b'));  // 4
console.log('C ->',get('C'));  // 3
console.log('d ->',get('d'));  // 2
console.log('f ->',get('f'));  // 0

console.log('empty string ->',get(''));  // -1 (actual Scott Marcus code return 1)
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {min-height:100% !important; top:0;}
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
0

Here the code you want but if you want to parse a number, use parseFloat/parseInt:

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
else
    return -1
}
eytienne
  • 103
  • 3
  • 11
-2
 function get (char)  {
  if (char == "A"|| char == "a") {
    return char = 5
  } else if (char == "B"|| char =="b") {
    return char = 4
  } else if (char == "C"|| char =="c") {
     return char = 3
  } else if (char == "D"|| char == "d") {
     return char = 2
  } else if (char == "F"|| char == "f") {
     return char = 0
  }

  return char;
}

I made a similar error when encoding paperio 3

AmonGames
  • 21
  • 3