Trying stuff to learn

Hello:

I find I am better able to learn and more fully comprehend the material when, in addition to required solution, I test being able to change to alternate code approach for same result. To that end, I completed this challenge and am working to duplicate in ‘conditional ternary’… i am not understanding what the error/failure in translation. I get similar syntax error on repl for this. the conditional ternary attempt starts about line 25 below. thank for your thoughts (yes, I know people don’t gen use or like ternary, not the point). again, thx you anyone that can provide some guidance for me to success identify problem/debug this alternate solution.

  **Your code so far**
// function caseInSwitch(val) {
//   let answer = "";
//   // Only change code below this line
// switch (val) {
//   case 1: 
//   answer = 'alpha';
//   break;
//   case 2: 
//   answer = 'beta';
//   break;
//   case 3: 
//   answer = 'gamma';
//   break;
//   case 4: 
//   answer = 'delta';
//   break;
// };
//   // Only change code above this line
//   return answer;
// }

// caseInSwitch(1);


function caseInSwitch(val) {
return val === 1 ? 'alpha'
: val === 2 ? 'beta'
: val === 3 ? 'gamma'
: val === 4 ? 'delta'

};

caseInSwitch(1);

FreeCodeCamp error message=
SyntaxError: unknown: Unexpected token, expected “:” (31:0)

29 | : val === 4 ? ‘delta’
30 |

31 | };
| ^
32 |
33 | caseInSwitch(1);

Repl IDE error
/home/runner/MessedUpFreeCodeCamp/index.js:99
: val === 4 ? ‘delta’;
^
SyntaxError: Unexpected token ‘;’

Challenge: Selecting from Many Options with Switch Statements

Link to the challenge:

Every ternary operator needs to have a semicolon. You don’t have a semicolon for the last one.

Also, FYI, you would never write production code like this. It’s OK to mess around if you are trying to understand the ternary operator better, but don’t even think about doing something like this in the real world :slight_smile:

1 Like

I believe @bbsmooth meant to say colon instead of semi-colon.

Also, if you are going to write some kind of complex ternary statement like this, it is easier to see what is missing by indenting the code.

function caseInSwitch(val) {
  return val === 1
    ? 'alpha'
    : val === 2
      ? 'beta'
      : val === 3
        ? 'gamma'
        : val === 4
          ? 'delta'
};
1 Like

Right you are. Thank you for the correction. Sorry for the confusion.

I guess I am still not seeing… I have reposted from original below, it does seem to have a colon??

function caseInSwitch(val) {
return val === 1 ? ‘alpha’
: val === 2 ? ‘beta’
: val === 3 ? ‘gamma’
: val === 4 ? ‘delta’
};

ok, this works-
function caseInSwitch(val) {
return val === 1
? ‘alpha’
: val === 2
? ‘beta’
: val === 3
? ‘gamma’
: val === 4
? ‘delta’
: ‘delta’

};

console.log(caseInSwitch(4));

To make it like the original using switch, you would want to return an empty string instead of delta for the value.