Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JavaScript Loop

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Basic for loop example improved</title>
    <style>

    </style>
  </head>
  <body>

  <p></p>


    <script>
    const cats = ['Bill', 'Jeff', 'Pete', 'Biggles', 'Jasmin'];
    let info = 'My cats are called ';
    const para = document.querySelector('p');

    for(let i = 0; i < cats.length; i++) {
      if(i === cats.length - 1) {
        info += 'and ' + cats[i] + '.';
      } else {
        info += cats[i] + ', ';
      }
    }

    para.textContent = info;

    </script>

  </body>
</html>
Source by raw.githubusercontent.com #
 
PREVIOUS NEXT
Tagged: #JavaScript #Loop
ADD COMMENT
Topic
Name
5+4 =