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

321
Views
¿Cómo puedo acceder a la variable 'noche' desde el ciclo while javascript?

 isValid = false; while (isValid == false) { let night = Number(window.prompt("pick a night")); if (typeof night == 'number' && night >= 1 && night <= 8) { isValid = true; } } document.write(night); //not accessible

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

0

Debes definir la variable night fuera del ciclo while, así:

 [...] let night let isValid = false; while(isValid == false) { night = Number(window.prompt("pick a night")); if(typeof night == 'number' && night >= 1 && night <= 8){ isValid = true; } } document.write(night); [...]
about 4 years ago · Juan Pablo Isaza Report

0

let es una variable de alcance de bloque, es decir, solo será accesible en el ciclo while. Si desea acceder a él fuera del ciclo while, declárelo como una variable var que es un ámbito global.

about 4 years ago · Juan Pablo Isaza Report

0

Para ser honesto, ni siquiera recomendaría usar un ciclo while.

Además, un problema con su código es que está verificando si typeof night === 'number' , pero siempre será un number ya que envolvió el valor en Number() . NaN en sí es del tipo de número.

 <script> const getNight = () => { const night = +window.prompt('Pick a night:'); if (isNaN(night) || night < 1 || night > 8) { alert('Try again.'); return getNight(); } return night; }; document.write(getNight()); </script>

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!