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 :

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>

Leave a Reply