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

127
Views
¿Qué me estoy perdiendo con estas funciones?

Tengo problemas con la validación de funciones. Necesito validar mis variables y asegurarme de que no puedas entrar sin dar un nombre o número. ¿Qué estoy haciendo mal para que esto no funcione?

 <script> var candyName = false; var pricePerLb; var numOfLbs; var message; candyName = prompt("Enter the name of the candy: "); while(candyName == false){ alert("Blank is invalid. Enter valid name: "); candyName = prompt("Enter the name of the candy: "); } pricePerLb = parseFloat(prompt("Enter the price per pound: ")); numOfLbs = parseInt(prompt("Enter the average number of pounds sold in a month: ")); if(numOfLbs > 2000 || pricePerLb > 10){ message = candyName + " that costs $" + pricePerLb + " is sold, on average, " + numOfLbs + " lbs a month are a best-selling item."; }else{ message = candyName + " are not a best-selling item."; } function validate(candyName){ if(candyName != ""){ valid = true; }else{ valid = false; alert("Blank is invalid: "); } return valid; alert(message); </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe verificar el retorno de su función de validación en lugar de candyName, ya que no es falso (puede estar vacío o indefinido pero no falso), vea este fragmento:

 var candyName = false; var pricePerLb; var numOfLbs; var message; candyName = prompt("Enter the name of the candy: "); while(validate(candyName) == false){ alert("Blank is invalid. Enter valid name: "); candyName = prompt("Enter the name of the candy: "); } pricePerLb = parseFloat(prompt("Enter the price per pound: ")); numOfLbs = parseInt(prompt("Enter the average number of pounds sold in a month: ")); if(numOfLbs > 2000 || pricePerLb > 10){ message = candyName + " that costs $" + pricePerLb + " is sold, on average, " + numOfLbs + " lbs a month are a best-selling item."; }else{ message = candyName + " are not a best-selling item."; } function validate(candyName){ if(candyName != ""){ valid = true; }else{ valid = false; } return valid; } alert(message)

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!