Simple memory game in JS

Developer
Size
2,500 Kb
Views
97,152

How do I make an simple memory game in js?

What is a simple memory game in js? How do you make a simple memory game in js? This script and codes were developed by James Barnett on 11 August 2022, Thursday.

Simple memory game in JS Previews

Simple memory game in JS - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Simple memory game in JS</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h1>LOLCatz Memory Game</h1>
<div id = "container"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Simple memory game in JS - Script Codes CSS Codes

h1 { font-size: 1.5em; margin-left: 44px;
}
#container { width: 800px; }
ul { padding: 0; margin: 0;}
li { padding: 0; float: left; margin: 10px; list-style: none; outline: solid; width: 150px; height: 150px;
}
img { width: 100%; height: 100%;
}

Simple memory game in JS - Script Codes JS Codes

var images = [];
// get images, place them in an array & randomize the order
for (var i = 0; i < 8; i++) { var rand = Math.floor(Math.random() * (1200 - 900 + 1) + 900); var img = 'http://lolcat.com/images/lolcats/' + rand + '.jpg'; images.push(img); images.push(img);
}
randomizeImages();
// output images then hide them
var output = "<ol>";
for (var i = 0; i < 16; i++) { output += "<li>"; output += "<img src = '" + images[i] + "'/>"; output += "</li>";
}
output += "</ol>";
document.getElementById("container").innerHTML = output;
$("img").hide();
var guess1 = "";
var guess2 = "";
var count = 0;
$("li").click(function() { if ((count < 2) && ($(this).children("img").hasClass("face-up")) === false) { // increment guess count, show image, mark it as face up count++; $(this).children("img").show(); $(this).children("img").addClass("face-up"); //guess #1 if (count === 1 ) { guess1 = $(this).children("img").attr("src"); } //guess #2 else { guess2 = $(this).children("img").attr("src"); // since it's the 2nd guess check for match if (guess1 === guess2) { console.log("match"); $("li").children("img[src='" + guess2 + "']").addClass("match"); } // else it's a miss else { console.log("miss"); setTimeout(function() { $("img").not(".match").hide(); $("img").not(".match").removeClass("face-up"); }, 1000); } // reset count = 0; setTimeout(function() { console.clear(); }, 60000); } }
});
// randomize array of images
function randomizeImages(){ Array.prototype.randomize = function() { var i = this.length, j, temp; while ( --i ) { j = Math.floor( Math.random() * (i - 1) ); temp = this[i]; this[i] = this[j]; this[j] = temp; } }; images.randomize();
}
Simple memory game in JS - Script Codes
Simple memory game in JS - Script Codes
Home Page Home
Developer James Barnett
Username jamesbarnett
Uploaded August 11, 2022
Rating 3.5
Size 2,500 Kb
Views 97,152
Do you need developer help for Simple memory game in JS?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

James Barnett (jamesbarnett) Script Codes
Create amazing captions with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!