Este es el código que he escrito hasta ahora.
<!DOCTYPE html> <html> <body> <script> { let hourNow = prompt("please enter the hour based on 24hr clock"); var greeting; if(hourNow = 6 && hourNow<=9){ greeting = 'Brreakfast is served.'; } else if (hourNow >= 11 && hourNow <=13){ greeting = 'Time for lunch.'; } else if (hourNow = 17 && hourNow >=20){ greeting = 'Time for dinner'; }else{ greeting = 'You will have to wait or get a snack' } } document.write('<h3>' + greeting + '</h3>'); </script> </body> </html>El código funciona y no funciona. Parece funcionar bien para el desayuno si ingresa 9. Todas las demás horas dice que lo siento, tiene que esperar o tomar un refrigerio.
use doble igual (comparación) en lugar de simple igual (asignación)
tampoco necesita usar >= mientras ya ha usado == . es redundante
también asumí que algunos casos aquí hay una respuesta fija
{ let hourNow = prompt("please enter the hour based on 24hr clock"); var greeting; if(hourNow >= 6 && hourNow<=9){ greeting = 'Brreakfast is served.'; } else if (hourNow >= 11 && hourNow <=13){ greeting = 'Time for lunch.'; } else if (hourNow >= 17 && hourNow <=20){ greeting = 'Time for dinner'; }else{ greeting = 'You will have to wait or get a snack' } }if(hourNow = 6 && hourNow<=9){siempre devuelve verdadero para que obtenga el resultado que está viendo.
Quieres hourNow == 6 no hourNow = 6
Gracias a @Michel por señalar que el mismo problema se aplica aquí:
} else if (hourNow = 17 && hourNow >=20){