Ternary Operation in Javascript

I've never bothered using ternary operation because it always seems that something could be written in something more familiar until now

The following line of code returns a student grade with the letter equivalent:
return `\nYou got a ${letterGrade} \(${(rawGrade*100).toFixed(2)}%\)!`

but it kind of bothers me that it gives a grammatically incorrect output if the grade is an a. So I changed the code into this:
return `\nYou got ${letterGrade == 'A' ? 'an' : 'a'} ${letterGrade} \(${(rawGrade*100).toFixed(2)}%\)!`

Just wrote it from memory and it actually worked!

Update (5/20/18): Another one and I'm getting used to this

let isValidPassword = function(password){
return (password.length < 8 || password.includes('password')) ? false : true
}

though this one is more elegant

let isValidPassword = function(password){
return password.length > 8 && !password.includes('password')
}

Comments

Popular posts from this blog

Blogging My Web Development Progress

PaintShop Pro