What is a boolean?

Question

What is a boolean?

Answer

A boolean value is one that’s either true or false.

1 Like

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()

1 Like

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;

}

}

2 Likes

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.

7 Likes

@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! :clap:t4: :clap:t4: :clap:t4:

2 Likes

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.

3 Likes

This is exactly how I did it, except that my parameter is called ‘number’

1 Like

Slightly different than yours,

const canIVote = (age) => {
return (age >= 18) ? true : false;
}

1 Like

@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.