Javascript Experiments
Javascript Experiments
try {
let radius = 7; // Trying to redeclare
console.log("This line won't execute due to
SyntaxError");
} catch (error) {
console.log("Error:", error.message);
}
try {
pi = 3.14; // Trying to reassign
console.log("This line won't execute");
} catch (error) {
console.log("Error:", error.message);
}
</script>
</body>
</html>
3.b) Problem Statement:
Write JavaScript code to display the movie details such as
movie name, language, and ratings. Initialize the variables with
values of appropriate types. Use template literals wherever
necessary.
Expected Output:
Starter Code:
<!DOCTYPE html>
<html>
<head>
<title>Movie Details</title>
<style>
div#maincontent {
height: 100px;
width: 500px;
border: 1px solid #CEE2FA;
text-align: left;
color: #08438E;
font-family: calibri;
font-size: 20;
padding: 5px;
}
div#heading {
text-decoration: bold;
text-align: center;
margin-top: 80px;
width: 500px;
border: 1px solid #CEE2FA;
text-align: center;
color: #08438E;
background-color: #CEE2FA;
font-family: calibri;
font-size: 20;
padding: 5px;
}
</style>
</head>
<body>
<center>
<div id="heading">Movie Details</div>
<div id="maincontent">
<script>
//Write the code to display the movie details
</script>
</div>
</center>
</body>
</html>
Script code
<script>
// Define constants
const pricePerTicket = 9;
const numberOfTickets = 3;
const discountPercentage = 10;
Script code:
<script>
function calculateBooking() {
const seatsToBook =
parseInt(document.getElementById('seats').value);
const ticketPrice = 9;
let totalPrice = 0;
let discount = 0;
let outputMessage = "";
</script>
<!DOCTYPE html>
<html>
<head>
<title>Booking Details</title>
<style>
div#maincontent {
height: auto;
width: 600px;
border: 1px solid #CEE2FA;
text-align: left;
color: #08438E;
font-family: calibri;
font-size: 20px;
padding: 5px;
}
div#heading {
text-align: center;
margin-top: 80px;
width: 600px;
border: 1px solid #CEE2FA;
color: #08438E;
background-color: #CEE2FA;
font-family: calibri;
font-size: 20px;
padding: 5px;
}
h4 {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<center>
<div id="heading">
<b>Theatre Drama</b>
</div>
<div id="maincontent">
<h4>Your Ticket Details:</h4>
<br>
<div>
<label for="seats">Enter the number of
seats to book:</label>
<input type="number" id="seats"
min="1" max="10" placeholder="Enter seats" required>
<button
onclick="calculateBooking()">Book Tickets</button>
</div>
<br>
<div id="output"></div>
</div>
</center>
<script>
const basePrice = 9;
function calculateBooking() {
const seats =
parseInt(document.getElementById('seats').value);
let output = "";
if (seats <= 2) {
output = `Total cost for ${seats}
tickets: $${seats * basePrice}`;
} else if (seats >= 6) {
output = "Booking not allowed for 6 or
more seats!";
} else {
output =
calculateWithDifferentLoops(seats);
}
document.getElementById('output').innerHTML = output;
}
function calculateWithDifferentLoops(seats) {
let output = "<h4>Using For Loop:</h4>";
output += calculateWithForLoop(seats);
return output;
}
function calculateWithForLoop(seats) {
let result = "";
let totalCost = 0;
function calculateWithDoWhileLoop(seats) {
let result = "";
let totalCost = 0;
let i = 0;
do {
const discount = 5 + (i * 2);
const discountedPrice = basePrice * (1
- discount/100);
totalCost += discountedPrice;
result += `Ticket for Customer ${i +
1} gets ${discount}% discount!, Cost is:
$${discountedPrice.toFixed(2)}<br>`;
i++;
} while (i < seats);