Answers for "how to make a loop javascript"

220

javascript for loop

var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) { 
  console.log(colors[i]);
}
Posted by: Guest on June-17-2019
31

javascript loop

let array = ['Item 1', 'Item 2', 'Item 3'];

array.forEach(item => {
	console.log(item); // Logs each 'Item #'
});
Posted by: Guest on February-20-2020
1

loop javascript

<body>

<h2>Automatic Slideshow</h2>
<p>Change image every 5 seconds:</p>


<div class="slideshow-container">
  <div class="mySlides fade">
    <img src="img_nature_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="img_snow_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="img_mountains_wide.jpg" style="width:100%">
  </div>
</div>

<br>



<div class="row">
    <div class="column">
      <img id="myBtn" class="demo cursor stop_loop" src="img_woods.jpg" style="width:100%" onclick="currentSlide(0); breakLoop();">
    </div>
    
    <div class="column">
      <img id="myBtn" class="demo cursor stop_loop" src="img_5terre.jpg" style="width:100%" onclick="currentSlide(1)">
    </div>
    
    <div class="column">
      <img id="myBtn" class="demo cursor stop_loop" src="img_mountains.jpg" style="width:100%" onclick="currentSlide(2)">
    </div>  
</div>





<script>
var slideIndex = 0; //indica che parto dall'immagine n.1
showSlides();


// script per lo slide delle immagini al click
function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}


// funzione per mostrare le slides
function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  var exitLoop = 0;   //interrompere il loop al click delle immagini
  
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  
  if (slideIndex > slides.length) {slideIndex = 1}    
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  } 
  
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
  
  

    enter code here

  if(exitLoop == 0) {
  	setTimeout(showSlides, 5000); // Change image every 5 seconds
  }

}
</script>

</body>
</html>
Posted by: Guest on September-12-2021
0

loops javascript

/*Loops are great tools when you need your program to run a code block a 
certain number of times or until a condition is met, but they need a 
terminal condition that ends the looping. Infinite loops are likely to 
freeze or crash the browser, and cause general program execution mayhem, 
which no one wants.

Infinite loop example(do not call this function!):*/
function loopy() {
  while(true) {
    console.log("Hello, world!");
  }
}

/*Loop example with terminal condition:*/
function myFunc() {
  for (let i = 1; i <= 4; i += 2) {
    console.log("Still going!");
  }
}
Posted by: Guest on February-03-2021
-1

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>
Posted by: Guest on July-13-2021

Code answers related to "how to make a loop javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language