Need help, brain freeze if and +?

Hello & Thanks ,
I’m having a brain freeze .
I can’t figure out why addition occurs on each if statement in changeImage function .

<!DOCTYPE html>
<html>
	<head>
		<title></title>
	</head>
<script> </script>
<body>
	<h5>Changing Image</h5>

<img src="background-image-01.jpg" id="myImage" />

<br>
    <input type="button" onclick="changeImage()" value="Change Image">

<script language="javascript">
    var imageCount = 0 ; var nextImage = "background-image-01.jpg" ;
function changeImage() {
	console.log("imageCount = " + imageCount  );  
    imageCount = imageCount + 1 ;
	console.log("imageCount = " + imageCount  );  
    elem = document.getElementById("myImage"); 
    if (imageCount = 1) {  nextImage = "background-image-02.jpg" ; elem.src = nextImage ; } 
	console.log("imageCount = " + imageCount  + "    nextImage = " + nextImage) ; 
    if (imageCount = 2) {  nextImage = "background-image-03.jpg" ; elem.src = nextImage ; } 
	console.log("imageCount = " + imageCount  + "    nextImage = " + nextImage) ; 
    if (imageCount = 3) {  nextImage = "background-image-04.jpg" ; elem.src = nextImage ; } 
	console.log("imageCount = " + imageCount  + "    nextImage = " + nextImage) ; 
    if (imageCount = 4) {  nextImage = "background-image-01.jpg" ; elem.src = nextImage ; } 
	console.log("imageCount = " + imageCount  + "    nextImage = " + nextImage) ; 
	if (imageCount = 4)  { imageCount = 0;}
	console.log("imageCount = " + imageCount  + "    nextImage = " + nextImage) ; 
  }
</script>
<p><strong>Note:</strong> Click on the "Change Image" button to changeImage</p>
</body>
</html>

You’ve confused = and ==. (we all do it from time to time.)

1 Like

Ugh! Thanks

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.