JavaScript Counter Project for beginners is made using HTML, CSS JavaScript.

Create a folder for your project and inside this folder create 3 files index.html, style.css and script.js and copy the code properly in each file.

HTML

				
					<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Counter</title>
	<link rel="stylesheet" href="style.css">
	<script type="text/javascript" src="script.js" defer=""></script>
</head>
<body>
	<div id="container">
		<div id="display" class="item">
			<p></p>
		</div>
		<button class="item" onclick="minus()">-</button>
		<button class="item" onclick="reset()">Reset</button>
		<button class="item" onclick="plus()">+</button>
	</div>
</body>
</html>
				
			

CSS

				
					body{
	background-image: linear-gradient(65deg, #85FFBD 0%, #FFFB7D 100%);
}
#container{
	display: grid;
	grid-template-columns: auto auto auto;
	width: 400px;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;	position: absolute;	
	padding: 20px;
}
#display{
	grid-column: 1/ span 3;
	background-color: rgba(0,0,0,0.7);
	color: white;
	font-family: montserrat, lato, san-serif;
	height: 200px;
	line-height: 150px;
	padding: 20px;
	box-sizing: border-box;
}
.item{
	font-size: 28px;
	border: 1px solid rgba(0,0,0, 0.2);
	margin: 2px;
	font-family: montserrat, lato, san-serif;
}
button:nth-child(2){
	padding:0 5px;
	grid-column: 1 / 3;
}
button:nth-child(4){
	grid-column: 1/span 3;
	height: 150px
}
p{
	margin: 0;
	box-sizing: border-box;
	border: 1px solid white;
	text-align: center;
	font-size: 32px;
	vertical-align: middle;
	height: 100%;
	width: 100%;
}
				
			

JavaScript

				
					let p = document.querySelector('p');
let n = 0;

function updateNumber() {
	return p.innerText = n;
}

function plus(){
	n++;
	return updateNumber();
}

function minus(){
	if(n > 0)n--;
	return updateNumber();
}

function reset(){
	n=0;
	return updateNumber();
}
				
			

Leave a Reply

Your email address will not be published.