Created a function in js using a ternary operator