Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Intentando obtener este código para mostrar un saludo cuando el usuario ingresa ciertas horas en un mensaje

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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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' } }
about 4 years ago · Juan Pablo Isaza Report

0

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){
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!