Can this code be cleaner / simpler?

Dear coders, I have questions: just to practice I made a little piece of code which tells the user to which generation he/she belongs :wink: The code works (thankfully, I’m struggling learning JavaScript!!), but

  1. I have the feeling it could be more cleaner / simpler: does anyone have suggestions? (I know the ‘switch’ statement, but I don’t know how to evaluate 2 values like in the if, else statement.

  2. I don’t know how to call the function jus like: myGeneration(2022, 1979), why do I have to write: var myGeneration(2022, 1979) ??

javascript snippet

switch statements are very useful when you need to match exact values. Which is not the case here, so using switch is not recommended.

I am somewhat concerned about this code, the decades (20, 30, 40, 50) do not look to go well. Unless that is intentional?

if you would use a function for the comparison, you could use return instead and get rid of all the double conditionals. Then you no longer need to verify that myGeneration is greater then x (20, 30 and so forth)

given the function would just hand back the string once a conditional has been met

If you need more help, please copy paste your code to the forum so we can actually run your code :wink:

you gave the function and the variable in which you store the result of the function call the same name? Why?

Thanks for this: here is the code… And strangely it doesn’t run in the Codebyte… :frowning: Not necessery to say I’m a complete newbie. :roll_eyes:

function myGeneration(year, birthyear) { return year - birthyear; } var myGeneration = myGeneration(2022, 1969); if (myGeneration < 20) { console.log(myGeneration + '! Teenager!!'); } else if (myGeneration > 20 && myGeneration < 30) { console.log(myGeneration + '! Up to thirty!!'); } else if (myGeneration > 30 && myGeneration < 40) { console.log(myGeneration + '! Quartercrisis!'); } else if (myGeneration > 40 && myGeneration < 50) { console.log(myGeneration + '! Midlife Crisis!!'); } else if (myGeneration > 50 && myGeneration < 60) { console.log(myGeneration + '! You are pre-old!!'); } else { console.log(myGeneration + '! Your life is just beginning!!'); } var myGeneration = myGeneration(2022, 1969);

That’s indeerd not handy to have the same name for function and variable…

then you overwrite your function, furthermore, if I do something like this:

// I renamed the function
var myGeneration = ageCalculator(2022, 1992);

I get:

"30! Your life is just beginning!!"

not sure that is the desired behavior?

ah, I have to us <= in stead of only <

or >= instead of >, depending on where you want the decades (20, 30, 40, 50) to be included

repeat of what I said earlier:

I’m sorry, I don’t get it 100%: how does that look like?

something like this:

   if (myGeneration < 20) {
     return myGeneration +  '! Teenager!!';
  } else if (myGeneration < 30) {
    // 
  }

given return will break the function, the else if clause is not reached. So the greater then check can be removed

Nice! I will try that!

What am I doing wrong??

function ageCalculator(year, birthyear) { return year - birthyear; if (myGeneration < 20) { return myGeneration + '! Teenager TEEN'; } else if (myGeneration <= 30) { return '! Up to thirty!!'; } else if (myGeneration <= 40) { return '! Quartercrisis!'; } else if (myGeneration <= 50) { return '! Midlife Crisis!!'; } else { return '! Your life is just beginning!!'; } } ageCalculator(2022, 2009);

well, return hands back data to the caller/function call, which signals that the function is done executing

so currently your function returns the age (year - birthyear)

you also forgot to .log() the returned result on ageCalculator function call

You don’t necessarily need the return. If the first if is true, the remaining else will not be execute.

So you could have something like this:

if (myGeneration < 20) {
  console.log('Teenager');
} else if (myGeneration < 30) {
  console.log('Thirty');
}

If myGeneration is less than 20, only the first one is execute.

Of course, I should have known. Thank you for this correction :slight_smile:

true, the remaining clauses/conditionals are not once one of the conditions evaluate to true

still nice to wrap your logic in a function though :wink:

Thank you! It works! :smiley: (when I paste the code, it doesn’t take the total code… :frowning: )

function ageCalculator(year, birthyear) { var myGeneration = year - birthyear; if (myGeneration < 20) { console.log('Teenager TEEN'); } else if (myGeneration <= 30) { console.log('Up to thirty!!'); } else if (myGeneration <= 40) { console.log('Quartercrisis!'); } else if (myGeneration <= 50) { console.log('Midlife Crisis!!'); } else { console.log('Your life is just beginning!!'); } } ageCalculator(2022, 1979);