Question
What is a boolean?
Answer
A boolean value is one that’s either true or false.
What is a boolean?
A boolean value is one that’s either true or false.
Hello Everyone, I want to verify if i pass this code before i proceed.
// Write your function here:
const canIVote = number => {
const age = number
if (age === 18) {
return true;
} else if (age >= 18) {
return true;
} else if (age < 18) {
return false;
} else {
return 'You most be 18 years or above';
}
}
console.log()
The code is good in my opinion. If you want you can shorten it a bit more like this:
const canIVote = age => {
if(age >= 18) {
return true;
} else {
return false;
}
}
You can shorten it even more, if it’s convenient:
const canIVote = age =>
age >= 18;
is enough, and will return true if age >= 18 and false if else.
@mariaburmeister that is a great solution!
I arrived at:
function canIVote(age) {
return (age > 18 ? true : false);
}
console.log(canIVote());
But yours is even shorter and easier!
I really love your approach. From the very beginning we’re told to think clearly and be concise with our code. However, another requirement to becoming a good programmer is being able to see redundant code and adhering to D.R.Y. (Don’t repeat yourself). In my opinion, you’re doing a great job as you’re trying to be as concise as possible.
This is exactly how I did it, except that my parameter is called ‘number’
Slightly different than yours,
const canIVote = (age) => {
return (age >= 18) ? true : false;
}
@mariaburmeister i love your method but it seems so simple that it confuses me. i usually love to read out my code as i write so it will make sense to me. Hopefully, i’ll become better skilled at this and will comfortably write out my code in this shorter form like yours.