Home > Back-end >  My while loop is not working on JavaScript
My while loop is not working on JavaScript

Time:11-06

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>

CodePudding user response:

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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related