0

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!

Rodhis
  • 19
  • 6
  • 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 values – Bravo Jun 09 '22 at 00:14
  • `${(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 Jun 09 '22 at 00:21
  • 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 true – Bravo Jun 09 '22 at 00:23
  • Hello! Sorry for the delay. `&&` worked. Thank you! But can you explain why `||` (or) doesn't work while `&&` (and) works in this case? – Rodhis Jun 11 '22 at 19:22
  • can you see why `h != 0 || h != 1` is always true? if you can't, then I suggest you brush up on you boolean algebra – Bravo Jun 12 '22 at 03:09
  • I looked up at some videos on boolean algebra but I'm still not 100% sure. But I understand that if I use `!=` then I should use `&&`, and if I use `= or ==` , then I should go with `||`. Thank you very much! – Rodhis Jun 13 '22 at 20:31
  • in THIS case, yes – Bravo Jun 14 '22 at 00:15

0 Answers0