Playing around with switch statements and arrow functions

Hey there! I am quite new here and posting this because i have some questions combining stuff i have learned so far.

I was busy with the following lesson: https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-practice-javascript-syntax-variables-data-types-conditionals-functions/lessons/javascript-fundamentals-code-challenge/exercises/life-phase

And was wondering how i could combine this with using switch statements instead of if else statements.

Below is a copy of my code (I have the original lesson in the top and am trying to do switch statements underneath, but it appears to not work correctly, only returning the default statement):

// Write your function here:

const lifePhase = (age) => {
if (age >= 0 && age <= 3) {
return ‘baby’;
}
if (age >= 4 && age <= 12) {
return ‘child’;
}
if (age >= 13 && age <= 19) {
return ‘teen’;
}
if (age >= 20 && age <= 64) {
return ‘adult’;
}
if (age >= 65 && age <= 140) {
return ‘senior citizen’;
}
if (age < 0 || age > 140) {
return ‘This is not a valid age’;
}
}

console.log(lifePhase(5)) //should print ‘child’

// We encourage you to add more function calls of your own to test your code!

const lifePhaseSwitch = (age) => {
switch (lifePhaseSwitch) {
case (age >= 0 && age <= 3):
return ‘baby’;
break;
case (age >= 4 && age <= 12):
return ‘child’;
break;
case (age >= 13 && age <= 19):
return ‘teen’;
break;
default:
return ‘wolla niffo’;
}
}

console.log(lifePhaseSwitch(2))

Hello, @giga9877395032, and welcome to the forums.

You are using switch (lifePhaseSwitch). The way switch...case works is that the evaluated value inside the parenthesis will be compared to the first case. If it matches, code execution resumes from there. If it doesn’t, the value is compared with the next case, and so on until the default is reached if it exists. What is the value of lifePhaseSwitch, and will it match any of your cases?

To do what you are attempting isn’t immediately straightforward. Your case's are fine, but don’t actually need upper and lower bounds. If we change the switch value to true, then the first case that is truthy will match, and control flow will be passed to it. For example:

const lowMedHigh = num => { switch (true) { case (num < 50): return `${num} is a low number.`; case (num < 500): return `${num} is a medium number.`; default: return `${num} is a high number.`; } } console.log(lowMedHigh(-12)); console.log(lowMedHigh(52)); console.log(lowMedHigh(501));

Thanks for your reply! I tried changing the switch statement parameter (If i am calling it correctly) to true, and the code seems to work! I am just trying to approach this using different methods since it is hard to code from the top of my mind. Trying to build a solid foundation of knowledge is quite difficult at this stage still.

Thanks for the reply! This really helped.

1 Like

I agree with the previous post;
switch (lifePhaseSwitch) {
should be
switch (age) {
because you want what the switch statement does to depend on the age variable.

Corrected in a post by @midlindner
[the thing inside a switch must be a boolean because each case is a boolean]

That won’t work with the case's as they are written. As they are currently written by @giga9877395032, they evaluate to truthy or falsy, so switching on age which is an integer, won’t produce expected results.