0
votes

I'm starting to learn JS and one of my tasks is to create a simple code to give the system time. I also wanted to increment it so it would give hour or hours based on what value the variable had (if it was 1, then the text is "hour"... if it was 2, it would be changed to "hours"... and so on)

With the help of someone from this site, I made the following:

const d = new Date()
const [h,m,s] = [d.getHours(), d.getMinutes(), d.getSeconds()]
divid.innerHTML = `Agora são ${h} hora${(h!=1)?"s":""}, ${m} minuto${(m!=1)?"s":""} e ${s} 
segundo${(s!=1)?"s":""}!`

My question is: is there a way to put another condition on the placeholder like it was used here? Like... ${(h!=1, h!=0)?"s":""} ? So I would get the "s" (plural) on the text if the variable is NOT 0 or 1?

I've tried with || and also using double parentheses [ like ${(h!=1)(h!=0)?"... etc ]. But it didn't work. Is there a way to do this? Or I have to use something like switch or .split() ?

Thank you!

so, like, maybe h<2 or (h !=0 && h != 1) - note, use && not || ... since h!=0 || h!=1 is ALWAYS true, since h can only be one value so it will always not equal one of two valuesBravo
${(h>1)?"s":""} works, but I want to know if there's a way to put more than one condition on the placeholder in this notation. I haven't tried ${(h!=1 && h!=0)?"s":""} so I'll be doing this next time. And in my country 0 hour, 0 minute and 0 second is most widely used and accepted by the grammar, and that's why I'm including 0 on singular in this code.Rodhis
yes .... you use && or || as appropriate .... you tried || which was inappropriate to the condition for the reason I described ... h != 0 || h != 1 is ALWAYS trueBravo