Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
How can I make an if statement include a range of numbers in javascript?

This is my first time ever coding and I'm having some trouble. Here is my code so far (javascript)

var userName = prompt("Hi! What is your name?"); // The answer the user gives for this prompt will be used later in the program

var hoursWorked = prompt("How many hours have you worked this week?"); //The answer the user gives for this prompt will be multiplied by 15 to determine the amount that they will be paid
var pay = ("$" + hoursWorked * 15)
var hoursLeft = (40 - hoursWorked); //This equation determines how many hours of work the user has left to complete
if (hoursLeft < 0) {
  alert("Uh oh! " + userName + "! That's too many hours!")
}; //If the user enters an amount of hours that is over 40 they will recieve an error message
if (hoursLeft === 0) {
  alert("Congratulations, " + userName + "! You have completed all 40 hours of work!"); //If the user has completed 40 hours of work they will be given this message
  alert(userName + ", your pay will be: " + pay)
};
if (hoursLeft => 0 && hoursLeft = < 39) {
  alert(userName + ", you have " + hoursLeft + " more hours to work this week."); //If the user has completed <40 hours of work they will be given this message, along with how many hours they have left to complete
  alert(userName + ", your pay so far is: " + pay)
};
if (hoursLeft > 40) {
  alert(userName + "! That is not possible!")
} //If the user enters a negative amount of hours they will be given this message

My problem is with this line "if (hoursLeft => 0 && hoursLeft =< 39 ){ "

How can I make it so if the hours are over 0 and less than 40 it will return the alerts? Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is you should use >= instead of => (this will be parsed as expression for arrow function) and use <= instead of =<.

<= Less than or equal operator.

>= Greater than or equal operator.

Check more here

var userName = prompt("Hi! What is your name?"); // The answer the user gives for this prompt will be used later in the program

var hoursWorked = prompt("How many hours have you worked this week?");  //The answer the user gives for this prompt will be multiplied by 15 to determine the amount that they will be paid
var pay = ("$" + hoursWorked * 15)  
var hoursLeft = (40 - hoursWorked); //This equation determines how many hours of work the user has left to complete
    if (hoursLeft < 0){
      alert ("Uh oh! " + userName + "! That's too many hours!")}; //If the user enters an amount of hours that is over 40 they will recieve an error message
    if (hoursLeft === 0){
      alert ("Congratulations, " + userName + "! You have completed all 40 hours of work!"); //If the user has completed 40 hours of work they will be given this message
      alert  ( userName + ", your pay will be: " + pay)}; 
    if (hoursLeft >= 0 && hoursLeft <= 39 ){  
       alert (userName + ", you have " + hoursLeft + " more hours to work this week."); //If the user has completed <40 hours of work they will be given this message, along with how many hours they have left to complete
      alert  ( userName + ", your pay so far is: " + pay)}; 
    if (hoursLeft > 40){
      alert (userName + "! That is not possible!")}

about 4 years ago · Juan Pablo Isaza Relatório

0

greater than or equal in js show with >= operator, and less than or equal show with <= operator, see this link for greater than and equal and this for less than and equal.

>=   greater than and equal

<=   less than and equal
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda