Carousel ERROR PLEASE CHECK MY CODE

hello,
i am actually trying to learn carousel with html css js and i have a doubt in this code i.e

code for automatice slideshow

var slideIndex = 0;
	carousel();

	function carousel() {
		var i;
		var x = document.getElementsByClassName("mySlides");
		var dots=document.getElementsByClassName("dot");

		for(i = 0; i < x.length; i++) {
			x[i].style.display = "none";
		}


		slideIndex++;

		if(slideIndex > x.length) {slideIndex = 1}
			for(i = 0; i < dots.length; i++) {
				dots[i].className = dots[i].className.replace(" active", "");
			}
x[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " active";
		setTimeout(carousel, 8000);

	}
// here my slideshow is working but i am getting here 1 error that is 

code2.js:31 Uncaught TypeError: Cannot read property ‘style’ of undefined
at showDivs (code2.js:31)
at code2.js:2

showDivs @ code2.js:31
(anonymous) @ code2.js:2

this is my code for slider 

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
	showDivs(slideIndex += n);
}

function currentDiv(n) {
	showDivs(slideIndex = n);
}

function showDivs(n) {
	var i;
	var x = document.getElementsByClassName("mySlides");
	var dots=document.getElementsByClassName("demo");

	if(n > x.length) {slideIndex = 1}

		if(n < 1) {slideIndex = x.length}

		for(i = 0; i < x.length; i++) {
        
        x[i].style.display = "none";

		}

		for(i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}

x[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}

I AM GETTING THE AUTOMATIC SLIDESHOW AND SLIDER BUT IT IS SHOWN IN THE CONSOLE THAT I HAVE 1 ERROR PLEASE TELL ME WHERE AM I WRONG

THANK YOU

Hello there,

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


Are you aware this function is incrementing slideIndex?

function plusDivs(n) {
    showDivs(slideIndex += n);
}

Otherwise, I recommend you do some debugging by console.loging any of the values within showDiv (particularly x), and see if anything is undefined.

Hope this helps

I will check and tell u Thank You

function plusDivs(n) {
showDivs(slideIndex += n);
}

i checked it with a value and its undefined can u please help me how can i solve this