Closed algusamine closed 3 years ago
This code isn't totally accurate. When you call for age fo 19 it will come up undefined. const age = 19;
let isAdult, canWork, canEnlist, canDrink;
if (age >= 21) { canWork = true; canEnlist = true; isAdult = true; canDrink = true; } else if (age >= 18) { canWork = true; canEnlist = true; isAdult = true; } else if (age >= 16) { canWork = true; } else { canDrink = false; canWork = false; canEnlist = false; isAdult = false; }
canDrink; //canEnlist; //isAdult; //canDrink;
The code should've been written this way instead: const age = -10;
if (age >= 0 && age < 16) { isAdult = false; canWork = false; canEnlist = false; canDrink = false; } else if ( age >= 16 && age < 21) { isAdult = false; canWork = true; canEnlist = true; canDrink = false; } else if (age >= 21) { isAdult = true; canWork = true; canEnlist = true; canDrink = true; } else { console.log('age must be above 0'); / and if it's too early to use (console.log) the code could end like this: } else { isAdult = true; canWork = true; canEnlist = true; canDrink = true; } / } canDrink; //canEnlist; //isAdult; //canDrink;
This code isn't totally accurate. When you call for age fo 19 it will come up undefined. const age = 19;
let isAdult, canWork, canEnlist, canDrink;
if (age >= 21) { canWork = true; canEnlist = true; isAdult = true; canDrink = true; } else if (age >= 18) { canWork = true; canEnlist = true; isAdult = true; } else if (age >= 16) { canWork = true; } else { canDrink = false; canWork = false; canEnlist = false; isAdult = false; }
canDrink; //canEnlist; //isAdult; //canDrink;
The code should've been written this way instead: const age = -10;
let isAdult, canWork, canEnlist, canDrink;
if (age >= 0 && age < 16) { isAdult = false; canWork = false; canEnlist = false; canDrink = false; } else if ( age >= 16 && age < 21) { isAdult = false; canWork = true; canEnlist = true; canDrink = false; } else if (age >= 21) { isAdult = true; canWork = true; canEnlist = true; canDrink = true; } else { console.log('age must be above 0'); / and if it's too early to use (console.log) the code could end like this: } else { isAdult = true; canWork = true; canEnlist = true; canDrink = true; } / } canDrink; //canEnlist; //isAdult; //canDrink;