Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My while loop is not working on JavaScript

my teacher sent me an excercise and I need help to solve it. It’s about making a shopping list. When you press the button cancel, the elements you add should be displayed on the website but it doesn’t happen. Please, I need help.

<body>
<h1>SHOPPING EVERYWHERE!</h1>
 <script>
 var lista = prompt('Introduce element to add');
  while (lista != null) {
   document.write('<li>' + lista + '</li>');
   l = prompt('Introduce element to add');
  }
 </script>
</body>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the variable „lista“ will never change in the loop because you never change the variable „lista“. I think your issue was that you wrote „l“ in the loop and not „lista“, so may write:

<body>
<h1>SHOPPING EVERYWHERE!</h1>
<script>
 var lista = prompt('Introduce element to add');
  while (lista != null) {
   document.write('<li>' + lista + '</li>');
   lista = prompt('Introduce element to add');
  }
 </script>
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading