title: Game about a car which has to cover 100km distance
body: <p>This is my assignment:</p>
  <blockquote>
  <p>There is a car with 30L of petrol.</p>
  <p>It needs to travel from 0 to 100 KMs.</p>
  <p>The car moves in steps, at each step car can move between 0 KM to 6 KM
  (generate a random number).</p>
  <p>The car has a mileage of 1 KM/L.</p>
  <p>You can place 5 random petrol pumps on the road.</p>
  <p>Every time the car reaches the petrol pump exactly, the car gets a
  refill of 20 L.</p>
  <p>Write a JavaScript code to run this game and print every step of the
  car with the current position, petrol remaining.</p>
  <p>Add a button on the page &quot;Start&quot; which should start the game. Print
  the result of the game in the document window.</p>
  <p>If the start button is clicked again, it should clear the window and
  start the game again.</p>
  </blockquote>
  <p>I want to check whether the car has passed a petrol pump or not if yes then add 20l , if not continue, but the code doesnt break out of the switch statement. My code below:</p>
  <pre><code>document.addEventListener('DOMContentLoaded', function () {
      const btn = document.getElementById('butt');
      btn.addEventListener('click', startTheGame);
      const rootbody = document.querySelector('.rootbody');
      const start = document.querySelector('.start');
  
      function startTheGame() {
          if (rootbody.innerHTML === '') {
              const header = document.createTextNode('Game Started!');
              start.appendChild(header);
              const p = {
                  PP1: 15,
                  PP2: 30,
                  PP3: 45,
                  PP4: 60,
                  PP5: 75,
              };
  
              const pps = document.createTextNode(
                  `Petrol Pumps generated at ${p.PP1}, ${p.PP2}, ${p.PP3}, ${p.PP4}, ${p.PP5} `
              );
              rootbody.appendChild(pps);
  
              let posi = 0;
              let rem = 30;
              while (posi &lt; 100) {
                  // Generate a random number
                  const random = Math.floor(Math.random() * 6 + 1);
                  // For every random number you generate, add it to posi to get the position
                  // till that point
                  posi = posi + random;
                  rem = parseInt(rem - random);
                  switch (posi) {
                      case 15:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 16:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 17:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 18:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 19:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 20:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      default:
                          break;
                  }
                  switch (posi) {
                      case 30:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 31:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 32:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 33:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 34:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 35:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      default:
                          break;
                  }
                  switch (posi.value) {
                      case 45:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 46:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 47:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 48:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 49:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 50:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      default:
                          break;
                  }
  
                  switch (posi) {
                      case 60:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 61:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 62:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 63:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 64:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 65:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 66:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      default:
                          break;
                  }
                  switch (posi) {
                      case 75:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 76:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 77:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 78:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 79:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      case 80:
                          rem += 20;
                          console.log(`petrol rem ${rem}`);
                          break;
                      default:
                          break;
                  }
                  // check whether you have reached the destination or not
                  // if reached break out of the loop and display destination
                  // reached!!!!
  
                  if (posi &gt;= 100) {
                      document.body.querySelector(
                          '.rootbody'
                      ).innerHTML += ` - ${random}Kms travelled, Destination reached`;
                      break;
                  }
  
                  // append the posi and the step
  
                  document.body.querySelector(
                      '.rootbody'
                  ).innerHTML += `&lt;br /&gt;Step - ${random}Kms travelled, position ${posi},
                  petrol remaining ${rem}L
                  &lt;br/&gt;`;
              }
          }
      }
  });
  </code></pre>