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

112
Views
Trying to get this code to display a greeting when user enters certain hours in a prompt

This is the code I have written so far.

<!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>

The code works and it doesn't work It seems to work ok for breakfast if you input 9. All other hours it is saying sorry you have to wait or get a snack.

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

0

use double equal (comparison) instead of single equal (assign)

also you don't need to use >= while you already have used ==. it's redundent

also I assumed some cases here is a fixed answer

{
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){

always returns true so you get the result you're seeing.

You want hourNow == 6 not hourNow = 6

Thanks to @Michel for pointing out that the same issue applies here:

} 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!