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

280
Views
JS: ¿Por qué mi do... while loop no se sale?

Quería ejecutar un bucle do...while para probar este código.

 var savings, chequing, action; savings = 1000; chequing = 1000; function checkAccounts() { alert("You have $" + savings + " in your savings and $" + chequing + " in your chequin account"); } function withdrawal() { let amount = +prompt("How much would you like to withdraw?"); //return the withdrawal value and store it in a variable called spending money return amount; } alert("Hello, welcome to the bank! What would you like to do?"); do { action = prompt("You can decide to see check your accounts (C), withdraw some money (W), or exit (E). Please choose one of those 3 actions"); console.log(action); if (action === 'C' || action === 'c') { checkAccounts(); } else if (action === 'W' || action === 'w') { let account = prompt("From which account would you like to withdraw some money? (S)avings or (C)hecking account"); if (account === 'S' || account === 's') { let spendingMoney = withdrawal(); savings -= spendingMoney; } else if (account === 'C' || account === 'c') { let spendingMoney = withdrawal(); chequing -= spendingMoney; } alert("After this operation, here are the details of your account :"); checkAccounts(); console.log(action); } console.log(action); } while (action !== 'E' || action !== 'e');

Mi objetivo es simplemente, cuando el usuario ingresa E cuando se le solicita, salimos del ciclo y cumplimos con la condición de que mientras la action no es E, seguimos rodando. No funciona con el código anterior. Estoy atascado con un bucle infinito incluso cuando ingresé E.

Puedo hacer que funcione si creo una nueva condición de instrucción if dentro del ciclo, como if action === 'E' {break} . Pero entonces no entiendo por qué la declaración while no vale nada.

todos los console.log(action) son para fines de depuración ...

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

0

Mira esta condición:

 while (action !== 'E' || action !== 'e');

Siempre será true :

 const action = 'E'; const isFulfilled = action => action !== 'E' || action !== 'e'; console.log(isFulfilled('E')); console.log(isFulfilled('e')); console.log(isFulfilled('x'));

Lo que necesitas es:

 while (action.toLowerCase() !== 'e');

O, menos legible:

 while (action !== 'E' && action !== 'e');
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!