Selecting from Many Options with Switch Statements - Why Basic Solution works correct?

Hi all,
Sorry for any mistakes. English is not my native language.

Tell us what’s happening:
After solving test I compared my answer with guide and I have a question.
In my solution I have rewritten answer from empty to alpha.

Why Basic Solution works correct? I don’t understand why return answer returns correct data when variable answer is still empty because switch case doesn’t rewrites answer value. Switch case just returns some value.

Then I decided to delete var answer = “”; and return answer; in Basic Solution and this solution works correctly too. Look at My solution 2

May be later there will be some explanation about how works return from function, but I want to know now: “Why return answer; works correct in Basic Solution?”

Thanks in advance!

My solution


function caseInSwitch(val) {
  var 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;  
}

// Change this value to test
caseInSwitch(1);

Basic Code Solution


function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
    case 1:
      return "alpha";
      break;
    case 2:
      return "beta";
      break;
    case 3:
      return "gamma";
      break;
    case 4:
      return "delta";
      break;
  }

  // Only change code above this line  
  return answer;  
}

// Change this value to test
caseInSwitch(1);

My Solution 2


function caseInSwitch(val) {
    // Only change code below this line
  switch(val) {
    case 1:
      return "alpha";
      break;
    case 2:
      return "beta";
      break;
    case 3:
      return "gamma";
      break;
    case 4:
      return "delta";
      break;
  }

  // Only change code above this line  
  }

// Change this value to test
caseInSwitch(1);

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements/
Link to the guide:
https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements/

Hi!

Why Basic Solution works correct?

It works because the Basic solution is returning a static value instead of a variable.

When the function caseInSwitch is called within the Basic Solution, the switch is evaluated and, since there is a return instead of other statement, the function returns the value and exists, stopping its execution. In other words, the first return a function finds stops the rest of the function from being executed.

For example, a perfectly valid function:

function myTestFunction(num) {
  if (num === 0) { // If num is zero
    return 'It was zero'; // Nothing else is executed
  }

  let result = '';

  switch (num) {
    case 1: // If num == 1
      return 'It was one'; // Anything below is not executed.
    case 2: // If num === 2
      result = 'It was two'; // Anything below is not executed
      break;
    default:
      return 'Not in case';
  }

  return result;
}

console.log(myTestFunction(0)); // Logs 'It was zero'
console.log(myTestFunction(1)); // It was one
console.log(myTestFunction(2)); // It was two
console.log(myTestFunction(3)); // Not in case
console.log(myTestFunction(4)); // Not in case

When called with 0, the function enters the first If and cancels the execution of the rest of the body, ignoring everything below the first return.

When called with 1, the first if is not executed, hence the execution continues; then the variable result is declared and initialized; the switch is evaluated and the first case is picked, and since there is a return statement, the function exists returning the static string ‘It was 1’.

When called with 2, the same as before happens, except this time, the case body alters the variable result and breaks out of the switch, finally returning the value of the result variable.

Finally, when called with any other value, the switch default is executed returning a static string ‘Not in case’.

If You notice, there are no breaks when I used a return inside the switch, because as already stated, everything after the return is ignored. There may be exceptions to this, for example within inline functions or arrow functions, but that’s because it’s another context.

What do You think the following code would log to the console?

function test(m) {
  if (m === true) {
    return 'The second one';
  }
  return 'The first one';
}

console.log(test(true));

I hope this helps You :slight_smile:

1 Like

The “return” stops the execution of the code. So in the “basic code solution” the return answer does nothing and actually should be deleted from the code (because it will never be executed because return is executed further up the code in the switch cases, this is a mistake in the code) and that is the confusing part. This is because in each case there is a “return” rather than “answer=” as in your first solution. So that is why when you delete return answer in your 2 solution it works.

1 Like

It would log to the console: The second one

@skaparate @eoja Thanks for explaining this to me.

1 Like