Practical 4
Practical 4
Practical 4
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Geolocation</title>
<style>
#loc-info{
font-size: 18px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Geolocation</h1>
<div id="loc-info">
<strong>Latitude</strong><span id="latitude"></span><br>
<strong>Longitude</strong><span id="longitude"></span>
</div>
<script>
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition,showError);
}else{
showError("Geolocation is not supported by this browser.");
}
function showPosition(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("latitude").textContent =
latitude.toFixed(6);
document.getElementById("longitude").textContent =
longitude.toFixed(6);
}
function showError(error){
var errorMessage;
switch(error.code){
case error.PERMISSION_DENIED:
errorMessage = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
errorMessage = "Location information is unavailable.";
break;
case error.TIMEOUT:
errorMessage = "The request to get user location timed
out.";
break;
case error.UNKNOWN_ERROR:
errorMessage = "An unknown error occurred.";
break;
default:
errorMessage = "An unspecified error occurred.";
break;
}
alert(errorMessage);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Management System</title>
</head>
<body>
<h1>Student Management System</h1>
<script>
function addStudent() {
var name = prompt("Enter student's name:");
var rollNo = prompt("Enter student's roll number:");
var gender = prompt("Enter student's gender:");
var student = { name: name, rollNo: rollNo, gender: gender };
var existingStudents =
JSON.parse(localStorage.getItem('students')) || [];
existingStudents.push(student);
localStorage.setItem('students',
JSON.stringify(existingStudents));
console.log("Student added:", student);
alert("Student added successfully!");
}
function getStudent() {
var rollNo = prompt("Enter student's roll number to retrieve
information:");
var existingStudents =
JSON.parse(localStorage.getItem('students')) || [];
var foundStudent = existingStudents.find(function(student) {
return student.rollNo === rollNo;
});
if (foundStudent) {
console.log("Student information:");
console.log("Name: " + foundStudent.name);
console.log("Roll No: " + foundStudent.rollNo);
console.log("Gender: " + foundStudent.gender);
alert("Student information retrieved successfully!");
} else {
console.log("Student not found.");
alert("Student not found.");
}
}
function removeStudent() {
var rollNo = prompt("Enter student's roll number to remove:");
var existingStudents =
JSON.parse(localStorage.getItem('students')) || [];
var updatedStudents = existingStudents.filter(function(student) {
return student.rollNo !== rollNo;
});
localStorage.setItem('students', JSON.stringify(updatedStudents));
console.log("Student with roll number '" + rollNo + "' removed
from storage.");
alert("Student removed successfully!");
}
function clearStorage() {
localStorage.removeItem('students');
console.log("Student data cleared from local storage.");
alert("Student data cleared successfully!");
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fruit Color Matching Game</title>
<style>
.draggable {
width: 100px;
height: 50px;
background-color: lightblue;
border: 2px solid darkblue;
margin: 10px;
padding: 10px;
cursor: move;
display: inline-block;
}
.dropZone {
width: 150px;
height: 100px;
margin: 10px;
padding: 10px;
display: inline-block;
}
#redDropZone {
background-color: rgb(247, 31, 31);
#yellowDropZone {
background-color: rgb(255, 255, 5);
#orangeDropZone {
background-color: rgb(255, 136, 1);
#message {
margin-top: 10px;
font-weight: bold;
color: darkgreen;
}
</style>
</head>
<body>
<h1>Fruit Color Matching Game</h1>
<div id="message"></div>
<script>
function dragStart(event) {
event.dataTransfer.setData("text/plain", event.target.id);
}
function allowDrop(event) {
event.preventDefault();
}
function drop(event) {
event.preventDefault();
dropZone.appendChild(draggedElement);
var correctMatches = {
"apple": "redDropZone",
"banana": "yellowDropZone",
"orange": "orangeDropZone"
};