Device Sync
This commit is contained in:
@@ -1,35 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lab 2</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Lab 2</h1>
|
||||
<script>
|
||||
/*
|
||||
* Name: Levi McLean
|
||||
* Project: Lab 1
|
||||
* Submission date: September 18th, 2025
|
||||
*/
|
||||
|
||||
var studentName; //unassigned variable
|
||||
do {
|
||||
studentName = prompt("Enter the student's name: "); //prompt for name, assign variable with value
|
||||
if (studentName === null || studentName.trim() === "") {
|
||||
alert("Please enter a student name");
|
||||
}
|
||||
} while (studentName === null || studentName.trim() === "");
|
||||
alert("Thank you for your entry!"); //Pop up message alert
|
||||
sessionStorage.setItem("Student Name", studentName);
|
||||
|
||||
const welcomeText = "Welcome to INFO-1272 Fall 2025 "; //set const welcomeText
|
||||
const nameText = `You are ${sessionStorage.getItem("Student Name")}`;
|
||||
document.write(welcomeText); //write const welcomeText
|
||||
document.write("<br>");
|
||||
document.write(nameText);
|
||||
</script>
|
||||
</body>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lab 2</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Lab 2</h1>
|
||||
<script>
|
||||
/*
|
||||
* Name: Levi McLean
|
||||
* Project: Lab 1
|
||||
* Submission date: September 18th, 2025
|
||||
*/
|
||||
|
||||
var studentName; //unassigned variable
|
||||
do {
|
||||
studentName = prompt("Enter the student's name: "); //prompt for name, assign variable with value
|
||||
if (studentName === null || studentName.trim() === "") {
|
||||
alert("Please enter a student name");
|
||||
}
|
||||
} while (studentName === null || studentName.trim() === "");
|
||||
alert("Thank you for your entry!"); //Pop up message alert
|
||||
sessionStorage.setItem("Student Name", studentName);
|
||||
|
||||
const welcomeText = "Welcome to INFO-1272 Fall 2025 "; //set const welcomeText
|
||||
const nameText = `You are ${sessionStorage.getItem("Student Name")}`;
|
||||
document.write(welcomeText); //write const welcomeText
|
||||
document.write("<br>");
|
||||
document.write(nameText);
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,81 +1,81 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lab 2</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Theme Park Tickets</h1>
|
||||
<script>
|
||||
// Constant values
|
||||
const CHILD_TICKET_PRICE = 9.99;
|
||||
const ADULT_TICKET_PRICE = 19.99;
|
||||
const SENIOR_TICKET_PRICE = 14.99;
|
||||
const HST_TAX = 0.13;
|
||||
|
||||
// Unassigned for now, assign later
|
||||
var ticketType;
|
||||
var ticketPrice;
|
||||
var userAge;
|
||||
var firstName;
|
||||
var lastName;
|
||||
var email;
|
||||
var phone;
|
||||
var tax;
|
||||
var total;
|
||||
var freeRides;
|
||||
|
||||
// Ask for user details
|
||||
firstName = prompt("First Name:");
|
||||
lastName = prompt("Last Name:");
|
||||
email = prompt("Email:");
|
||||
phone = prompt("Phone:");
|
||||
userAge = prompt("Enter your age:");
|
||||
|
||||
// Determine ticket price
|
||||
if (userAge < 12) {
|
||||
ticketPrice = CHILD_TICKET_PRICE;
|
||||
ticketType = "Child";
|
||||
} else if (userAge <= 59) {
|
||||
ticketPrice = ADULT_TICKET_PRICE;
|
||||
ticketType = "Adult";
|
||||
} else {
|
||||
ticketPrice = SENIOR_TICKET_PRICE;
|
||||
ticketType = "Senior";
|
||||
}
|
||||
|
||||
// Determine free ride coupons
|
||||
if (ticketType === "Child") {
|
||||
freeRides = 0;
|
||||
} else if (ticketType === "Adult") {
|
||||
freeRides = 2;
|
||||
} else {
|
||||
freeRides = 1;
|
||||
}
|
||||
|
||||
// Caluclate tax and total
|
||||
tax = ticketPrice * HST_TAX;
|
||||
total = ticketPrice + tax;
|
||||
|
||||
// Display to console
|
||||
console.log("==========================================");
|
||||
console.log(" Theme Park Ticket Receipt ");
|
||||
console.log("==========================================");
|
||||
console.log("Customer Name: ", firstName, lastName);
|
||||
console.log("Email: ", email);
|
||||
console.log("Phone: ", phone);
|
||||
console.log("------------------------------------------");
|
||||
console.log("Ticket Type: ", ticketType);
|
||||
console.log("Base Price: $", ticketPrice.toFixed(2));
|
||||
console.log("Tax (13%): $", tax.toFixed(2));
|
||||
console.log("TOTAL: $", total.toFixed(2));
|
||||
console.log("------------------------------------------");
|
||||
console.log("Free Ride Coupons Earned: ", freeRides);
|
||||
console.log("==========================================");
|
||||
console.log(" Have a fun and safe day at the park! ");
|
||||
console.log("==========================================");
|
||||
</script>
|
||||
</body>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lab 2</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Theme Park Tickets</h1>
|
||||
<script>
|
||||
// Constant values
|
||||
const CHILD_TICKET_PRICE = 9.99;
|
||||
const ADULT_TICKET_PRICE = 19.99;
|
||||
const SENIOR_TICKET_PRICE = 14.99;
|
||||
const HST_TAX = 0.13;
|
||||
|
||||
// Unassigned for now, assign later
|
||||
var ticketType;
|
||||
var ticketPrice;
|
||||
var userAge;
|
||||
var firstName;
|
||||
var lastName;
|
||||
var email;
|
||||
var phone;
|
||||
var tax;
|
||||
var total;
|
||||
var freeRides;
|
||||
|
||||
// Ask for user details
|
||||
firstName = prompt("First Name:");
|
||||
lastName = prompt("Last Name:");
|
||||
email = prompt("Email:");
|
||||
phone = prompt("Phone:");
|
||||
userAge = prompt("Enter your age:");
|
||||
|
||||
// Determine ticket price
|
||||
if (userAge < 12) {
|
||||
ticketPrice = CHILD_TICKET_PRICE;
|
||||
ticketType = "Child";
|
||||
} else if (userAge <= 59) {
|
||||
ticketPrice = ADULT_TICKET_PRICE;
|
||||
ticketType = "Adult";
|
||||
} else {
|
||||
ticketPrice = SENIOR_TICKET_PRICE;
|
||||
ticketType = "Senior";
|
||||
}
|
||||
|
||||
// Determine free ride coupons
|
||||
if (ticketType === "Child") {
|
||||
freeRides = 0;
|
||||
} else if (ticketType === "Adult") {
|
||||
freeRides = 2;
|
||||
} else {
|
||||
freeRides = 1;
|
||||
}
|
||||
|
||||
// Caluclate tax and total
|
||||
tax = ticketPrice * HST_TAX;
|
||||
total = ticketPrice + tax;
|
||||
|
||||
// Display to console
|
||||
console.log("==========================================");
|
||||
console.log(" Theme Park Ticket Receipt ");
|
||||
console.log("==========================================");
|
||||
console.log("Customer Name: ", firstName, lastName);
|
||||
console.log("Email: ", email);
|
||||
console.log("Phone: ", phone);
|
||||
console.log("------------------------------------------");
|
||||
console.log("Ticket Type: ", ticketType);
|
||||
console.log("Base Price: $", ticketPrice.toFixed(2));
|
||||
console.log("Tax (13%): $", tax.toFixed(2));
|
||||
console.log("TOTAL: $", total.toFixed(2));
|
||||
console.log("------------------------------------------");
|
||||
console.log("Free Ride Coupons Earned: ", freeRides);
|
||||
console.log("==========================================");
|
||||
console.log(" Have a fun and safe day at the park! ");
|
||||
console.log("==========================================");
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,61 +1,61 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Lab 3</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Lab 3</h1>
|
||||
<script>
|
||||
/*
|
||||
* I have decided to declare my fortune messages in order of
|
||||
* most-to-least positive
|
||||
*/
|
||||
var fortune1 = "You will have a great day";
|
||||
var fortune2 = "Success if around the corner";
|
||||
var fortune3 = "Happiness comes from within";
|
||||
var fortune4 = "Today will be okay";
|
||||
var fortune5 = "Keep an eye on the details";
|
||||
var fortune6 = "Patience will be tested";
|
||||
var fortune7 = "You will step on LEGO soon";
|
||||
var fortune8 = "Beware of pigeons, they have attitude";
|
||||
var fortune9 = "Your WIFI will mysteriously stop working";
|
||||
|
||||
// Prompt user for numCookies
|
||||
var numCookies = window.prompt("How many cookies do you want to open?");
|
||||
|
||||
// Loop starting at 1, stoping at numCookies
|
||||
for (var i = 1; i <=numCookies; i++) {
|
||||
|
||||
// Generate random number from 1 - 9 inclusive
|
||||
var randNum = Math.floor(Math.random() * 9) + 1;
|
||||
|
||||
// Assign messge and cateogry to blank for now
|
||||
var message = ""
|
||||
var category = ""
|
||||
|
||||
// Determine message and category from randNum
|
||||
if (randNum <= 3) {
|
||||
if (randNum === 1) message = fortune1;
|
||||
else if (randNum === 2) message = fortune2;
|
||||
else message = fortune3;
|
||||
category = "Positive"; // Positive if 1-3
|
||||
} else if (randNum <= 6) {
|
||||
if (randNum === 4) message = fortune4;
|
||||
else if (randNum === 5) message = fortune5;
|
||||
else message = fortune6;
|
||||
category = "Neutral"; // Neutral if 4-6
|
||||
} else {
|
||||
if (randNum === 7) message = fortune7;
|
||||
else if (randNum === 8) message = fortune8;
|
||||
else message = fortune9;
|
||||
category = "Funny" // Funny if 7-9
|
||||
}
|
||||
|
||||
// Display fortune cookie details
|
||||
console.log("Cookie #" + i + "\nMessage: " + message + "\nCategory: " + category);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Lab 3</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Lab 3</h1>
|
||||
<script>
|
||||
/*
|
||||
* I have decided to declare my fortune messages in order of
|
||||
* most-to-least positive
|
||||
*/
|
||||
var fortune1 = "You will have a great day";
|
||||
var fortune2 = "Success if around the corner";
|
||||
var fortune3 = "Happiness comes from within";
|
||||
var fortune4 = "Today will be okay";
|
||||
var fortune5 = "Keep an eye on the details";
|
||||
var fortune6 = "Patience will be tested";
|
||||
var fortune7 = "You will step on LEGO soon";
|
||||
var fortune8 = "Beware of pigeons, they have attitude";
|
||||
var fortune9 = "Your WIFI will mysteriously stop working";
|
||||
|
||||
// Prompt user for numCookies
|
||||
var numCookies = window.prompt("How many cookies do you want to open?");
|
||||
|
||||
// Loop starting at 1, stoping at numCookies
|
||||
for (var i = 1; i <=numCookies; i++) {
|
||||
|
||||
// Generate random number from 1 - 9 inclusive
|
||||
var randNum = Math.floor(Math.random() * 9) + 1;
|
||||
|
||||
// Assign messge and cateogry to blank for now
|
||||
var message = ""
|
||||
var category = ""
|
||||
|
||||
// Determine message and category from randNum
|
||||
if (randNum <= 3) {
|
||||
if (randNum === 1) message = fortune1;
|
||||
else if (randNum === 2) message = fortune2;
|
||||
else message = fortune3;
|
||||
category = "Positive"; // Positive if 1-3
|
||||
} else if (randNum <= 6) {
|
||||
if (randNum === 4) message = fortune4;
|
||||
else if (randNum === 5) message = fortune5;
|
||||
else message = fortune6;
|
||||
category = "Neutral"; // Neutral if 4-6
|
||||
} else {
|
||||
if (randNum === 7) message = fortune7;
|
||||
else if (randNum === 8) message = fortune8;
|
||||
else message = fortune9;
|
||||
category = "Funny" // Funny if 7-9
|
||||
}
|
||||
|
||||
// Display fortune cookie details
|
||||
console.log("Cookie #" + i + "\nMessage: " + message + "\nCategory: " + category);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,93 +1,93 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lab 5</title>
|
||||
<meta charset="utf-8" lang="en">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Lab 5</h1>
|
||||
<script>
|
||||
// Get number of students in class
|
||||
var numStudents = parseInt(window.prompt("How many students are in the class?"));
|
||||
var studentNames = [];
|
||||
var studentGrades = [];
|
||||
|
||||
// Run grade analytics once so theres something to display
|
||||
gradeAnalyzer();
|
||||
calculateAverage(studentGrades);
|
||||
findMinMax(studentGrades);
|
||||
|
||||
// Ask user if they want to review the results
|
||||
do {
|
||||
displayResults();
|
||||
var viewAgain = window.confirm("Would you like to view the results again?");
|
||||
} while (viewAgain);
|
||||
|
||||
// Thank user and end loop
|
||||
alert("Thank you for using the Grade Analyzer!");
|
||||
|
||||
// Function Definitions from here on
|
||||
|
||||
// Gets names, grades and inserts into respective arrays
|
||||
function gradeAnalyzer() {
|
||||
for (var i = 0; i <= numStudents - 1; i++) {
|
||||
var studentName = window.prompt("Enter students name:");
|
||||
var studentGrade = parseFloat(window.prompt("Enter student grade:"));
|
||||
if (studentGrade > 0 && studentGrade <= 100) {
|
||||
studentNames.push(studentName);
|
||||
studentGrades.push(studentGrade);
|
||||
} else {
|
||||
alert("Invalid grade entered, skipping entry");
|
||||
}
|
||||
console.log("Student " + (i + 1) + ": " + studentName + " - Grade: " + studentGrade);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculates the average of a given array
|
||||
function calculateAverage(arr) {
|
||||
var totalGrades = 0;
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
totalGrades += arr[i];
|
||||
}
|
||||
var average = totalGrades / arr.length;
|
||||
console.log("Class average: " + average.toFixed(2));
|
||||
}
|
||||
|
||||
// Finds the minimum and maximum value of an array by looping through it
|
||||
function findMinMax(arr) {
|
||||
if (arr.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
var min = arr[0];
|
||||
var max = arr[0];
|
||||
|
||||
for (var i = 1; i < arr.length; i++) {
|
||||
var grade = arr[i];
|
||||
if (grade < min) {
|
||||
min = grade;
|
||||
}
|
||||
if (grade > max) {
|
||||
max = grade;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Min: " + min + " Max: " + max);
|
||||
}
|
||||
|
||||
// Helper function to display results cleanly
|
||||
function displayResults() {
|
||||
console.clear();
|
||||
console.log("Student List:");
|
||||
for (var i = 0; i < studentNames.length; i++) {
|
||||
console.log("Student " + i + ": " + studentNames[i] + " Grade: " + studentGrades[i]);
|
||||
}
|
||||
calculateAverage(studentGrades);
|
||||
findMinMax(studentGrades);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Lab 5</title>
|
||||
<meta charset="utf-8" lang="en">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Lab 5</h1>
|
||||
<script>
|
||||
// Get number of students in class
|
||||
var numStudents = parseInt(window.prompt("How many students are in the class?"));
|
||||
var studentNames = [];
|
||||
var studentGrades = [];
|
||||
|
||||
// Run grade analytics once so theres something to display
|
||||
gradeAnalyzer();
|
||||
calculateAverage(studentGrades);
|
||||
findMinMax(studentGrades);
|
||||
|
||||
// Ask user if they want to review the results
|
||||
do {
|
||||
displayResults();
|
||||
var viewAgain = window.confirm("Would you like to view the results again?");
|
||||
} while (viewAgain);
|
||||
|
||||
// Thank user and end loop
|
||||
alert("Thank you for using the Grade Analyzer!");
|
||||
|
||||
// Function Definitions from here on
|
||||
|
||||
// Gets names, grades and inserts into respective arrays
|
||||
function gradeAnalyzer() {
|
||||
for (var i = 0; i <= numStudents - 1; i++) {
|
||||
var studentName = window.prompt("Enter students name:");
|
||||
var studentGrade = parseFloat(window.prompt("Enter student grade:"));
|
||||
if (studentGrade > 0 && studentGrade <= 100) {
|
||||
studentNames.push(studentName);
|
||||
studentGrades.push(studentGrade);
|
||||
} else {
|
||||
alert("Invalid grade entered, skipping entry");
|
||||
}
|
||||
console.log("Student " + (i + 1) + ": " + studentName + " - Grade: " + studentGrade);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculates the average of a given array
|
||||
function calculateAverage(arr) {
|
||||
var totalGrades = 0;
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
totalGrades += arr[i];
|
||||
}
|
||||
var average = totalGrades / arr.length;
|
||||
console.log("Class average: " + average.toFixed(2));
|
||||
}
|
||||
|
||||
// Finds the minimum and maximum value of an array by looping through it
|
||||
function findMinMax(arr) {
|
||||
if (arr.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
var min = arr[0];
|
||||
var max = arr[0];
|
||||
|
||||
for (var i = 1; i < arr.length; i++) {
|
||||
var grade = arr[i];
|
||||
if (grade < min) {
|
||||
min = grade;
|
||||
}
|
||||
if (grade > max) {
|
||||
max = grade;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Min: " + min + " Max: " + max);
|
||||
}
|
||||
|
||||
// Helper function to display results cleanly
|
||||
function displayResults() {
|
||||
console.clear();
|
||||
console.log("Student List:");
|
||||
for (var i = 0; i < studentNames.length; i++) {
|
||||
console.log("Student " + i + ": " + studentNames[i] + " Grade: " + studentGrades[i]);
|
||||
}
|
||||
calculateAverage(studentGrades);
|
||||
findMinMax(studentGrades);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,130 +1,130 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Levi McLean Midterm Part B</title>
|
||||
<meta charset="utf-8" lang="en-us">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Levi McLean Midterm Part B</h1>
|
||||
<script>
|
||||
const TAX_RATE = 0.13;
|
||||
const DISCOUNT_RATE = 0.1;
|
||||
|
||||
var item1 = {
|
||||
name: "apple",
|
||||
price: 1.1,
|
||||
qty: 4
|
||||
};
|
||||
|
||||
var item2 = {
|
||||
name: "banana",
|
||||
price: 0.59,
|
||||
qty: 2
|
||||
};
|
||||
|
||||
var item3 = {
|
||||
name: "pear",
|
||||
price: 1.25,
|
||||
qty: 6
|
||||
};
|
||||
|
||||
var item4 = {
|
||||
name: "grapes",
|
||||
price: 2.25,
|
||||
qty: 8
|
||||
};
|
||||
|
||||
var item5 = {
|
||||
name: "kiwi",
|
||||
price: 3,
|
||||
qty: 1
|
||||
};
|
||||
|
||||
var cart = [item1, item2, item3, item4, item5];
|
||||
|
||||
function searchItem() {
|
||||
var itemName = window.prompt("Enter an item name: ").toLowerCase();
|
||||
var foundItem;
|
||||
|
||||
if (itemName == null) {
|
||||
window.alert("Item name cannot be null.");
|
||||
}
|
||||
|
||||
for (var i=0; i < cart.length; i++) {
|
||||
if (cart[i].name === itemName) {
|
||||
window.alert("Item(s) found.");
|
||||
foundItem = cart[i];
|
||||
return foundItem;
|
||||
}
|
||||
}
|
||||
window.alert("Item(s) not found.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function updateItemQuantity() {
|
||||
var updateItem = window.prompt("Enter item name to update: ").toLowerCase();
|
||||
|
||||
if (updateItem == null) {
|
||||
window.alert("Invalid item name.");
|
||||
}
|
||||
|
||||
for (var i=0; i < cart.length; i++) {
|
||||
if (cart[i].name === updateItem) {
|
||||
var updateQty = parseInt(window.prompt("How many items do we add? "), 10);
|
||||
cart[i].qty += updateQty;
|
||||
console.log(cart);
|
||||
return updateItem;
|
||||
}
|
||||
}
|
||||
window.alert("Item does not exist in cart.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function clearCart() {
|
||||
for (var i = 0; i < cart.length; i++) {
|
||||
cart[i].name = "";
|
||||
cart[i].qty = 0;
|
||||
}
|
||||
console.log(cart);
|
||||
window.alert("Cart has been cleared!");
|
||||
}
|
||||
|
||||
function calulateFinalPriceWithDiscount() {
|
||||
var subtotal = 0;
|
||||
for (var i = 0; i < cart.length; i++) {
|
||||
subtotal += cart[i].price * cart[i].qty;
|
||||
}
|
||||
var tax = subtotal * TAX_RATE;
|
||||
var discount = subtotal * DISCOUNT_RATE;
|
||||
var total = subtotal - discount + tax;
|
||||
window.alert(
|
||||
"Subtotal: $" + subtotal.toFixed(2) + "\n" +
|
||||
"Tax: $" + tax.toFixed(2) + "\n" +
|
||||
"Discount: $" + discount.toFixed(2) + "\n" +
|
||||
"Total: $" + total.toFixed(2)
|
||||
);
|
||||
}
|
||||
|
||||
var notDone = true;
|
||||
while(notDone === true) {
|
||||
var userChoice = window.prompt("Please make a selection (s)earch, (u)pdate, (c)lear, (d)iscount: ").toLowerCase();
|
||||
if (userChoice === "s") {
|
||||
searchItem();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "u") {
|
||||
updateItemQuantity();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "c") {
|
||||
clearCart();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "d") {
|
||||
calulateFinalPriceWithDiscount();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else {
|
||||
window.alert("Invalid selection, try again.");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Levi McLean Midterm Part B</title>
|
||||
<meta charset="utf-8" lang="en-us">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Levi McLean Midterm Part B</h1>
|
||||
<script>
|
||||
const TAX_RATE = 0.13;
|
||||
const DISCOUNT_RATE = 0.1;
|
||||
|
||||
var item1 = {
|
||||
name: "apple",
|
||||
price: 1.1,
|
||||
qty: 4
|
||||
};
|
||||
|
||||
var item2 = {
|
||||
name: "banana",
|
||||
price: 0.59,
|
||||
qty: 2
|
||||
};
|
||||
|
||||
var item3 = {
|
||||
name: "pear",
|
||||
price: 1.25,
|
||||
qty: 6
|
||||
};
|
||||
|
||||
var item4 = {
|
||||
name: "grapes",
|
||||
price: 2.25,
|
||||
qty: 8
|
||||
};
|
||||
|
||||
var item5 = {
|
||||
name: "kiwi",
|
||||
price: 3,
|
||||
qty: 1
|
||||
};
|
||||
|
||||
var cart = [item1, item2, item3, item4, item5];
|
||||
|
||||
function searchItem() {
|
||||
var itemName = window.prompt("Enter an item name: ").toLowerCase();
|
||||
var foundItem;
|
||||
|
||||
if (itemName == null) {
|
||||
window.alert("Item name cannot be null.");
|
||||
}
|
||||
|
||||
for (var i=0; i < cart.length; i++) {
|
||||
if (cart[i].name === itemName) {
|
||||
window.alert("Item(s) found.");
|
||||
foundItem = cart[i];
|
||||
return foundItem;
|
||||
}
|
||||
}
|
||||
window.alert("Item(s) not found.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function updateItemQuantity() {
|
||||
var updateItem = window.prompt("Enter item name to update: ").toLowerCase();
|
||||
|
||||
if (updateItem == null) {
|
||||
window.alert("Invalid item name.");
|
||||
}
|
||||
|
||||
for (var i=0; i < cart.length; i++) {
|
||||
if (cart[i].name === updateItem) {
|
||||
var updateQty = parseInt(window.prompt("How many items do we add? "), 10);
|
||||
cart[i].qty += updateQty;
|
||||
console.log(cart);
|
||||
return updateItem;
|
||||
}
|
||||
}
|
||||
window.alert("Item does not exist in cart.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function clearCart() {
|
||||
for (var i = 0; i < cart.length; i++) {
|
||||
cart[i].name = "";
|
||||
cart[i].qty = 0;
|
||||
}
|
||||
console.log(cart);
|
||||
window.alert("Cart has been cleared!");
|
||||
}
|
||||
|
||||
function calulateFinalPriceWithDiscount() {
|
||||
var subtotal = 0;
|
||||
for (var i = 0; i < cart.length; i++) {
|
||||
subtotal += cart[i].price * cart[i].qty;
|
||||
}
|
||||
var tax = subtotal * TAX_RATE;
|
||||
var discount = subtotal * DISCOUNT_RATE;
|
||||
var total = subtotal - discount + tax;
|
||||
window.alert(
|
||||
"Subtotal: $" + subtotal.toFixed(2) + "\n" +
|
||||
"Tax: $" + tax.toFixed(2) + "\n" +
|
||||
"Discount: $" + discount.toFixed(2) + "\n" +
|
||||
"Total: $" + total.toFixed(2)
|
||||
);
|
||||
}
|
||||
|
||||
var notDone = true;
|
||||
while(notDone === true) {
|
||||
var userChoice = window.prompt("Please make a selection (s)earch, (u)pdate, (c)lear, (d)iscount: ").toLowerCase();
|
||||
if (userChoice === "s") {
|
||||
searchItem();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "u") {
|
||||
updateItemQuantity();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "c") {
|
||||
clearCart();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "d") {
|
||||
calulateFinalPriceWithDiscount();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else {
|
||||
window.alert("Invalid selection, try again.");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,138 +1,138 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Levi McLean Midterm Part B</title>
|
||||
<meta charset="utf-8" lang="en-us">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Levi McLean Midterm Part B</h1>
|
||||
<script>
|
||||
const TAX_RATE = 0.13;
|
||||
const DISCOUNT_RATE = 0.1;
|
||||
|
||||
var item1 = {
|
||||
name: "apple",
|
||||
price: 1.1,
|
||||
qty: 4
|
||||
};
|
||||
|
||||
var item2 = {
|
||||
name: "banana",
|
||||
price: 0.59,
|
||||
qty: 2
|
||||
};
|
||||
|
||||
var item3 = {
|
||||
name: "pear",
|
||||
price: 1.25,
|
||||
qty: 6
|
||||
};
|
||||
|
||||
var item4 = {
|
||||
name: "grapes",
|
||||
price: 2.25,
|
||||
qty: 8
|
||||
};
|
||||
|
||||
var item5 = {
|
||||
name: "kiwi",
|
||||
price: 3,
|
||||
qty: 1
|
||||
};
|
||||
|
||||
var cart = [item1, item2, item3, item4, item5];
|
||||
|
||||
function searchItem() {
|
||||
var itemName = window.prompt("Enter an item name: ").toLowerCase();
|
||||
var foundItem;
|
||||
|
||||
if (itemName == null) {
|
||||
window.alert("Item name cannot be null.");
|
||||
}
|
||||
|
||||
for (var i=0; i <= cart.length; i++) {
|
||||
if (cart[i].name === itemName) {
|
||||
window.alert("Item(s) found.");
|
||||
foundItem = cart[i];
|
||||
return foundItem;
|
||||
}
|
||||
}
|
||||
window.alert("Item(s) not found.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function updateItemQuantity() {
|
||||
var updateItem = window.prompt("Enter item name to update: ").toLowerCase();
|
||||
|
||||
if (updateItem == null) {
|
||||
window.alert("Invalid item name.");
|
||||
}
|
||||
|
||||
for (var i=0; i <= cart.length; i++) {
|
||||
if (cart[i].name === updateItem) {
|
||||
var updateQty = parseInt(window.prompt("How many items do we add? "), 10);
|
||||
cart[i].qty += updateQty;
|
||||
console.log(cart);
|
||||
return updateItem;
|
||||
}
|
||||
window.alert("Item does not exist in cart.");
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function clearCart() {
|
||||
cart[0].name = "";
|
||||
cart[0].qty = 0;
|
||||
cart[1].name = "";
|
||||
cart[1].qty = 0;
|
||||
cart[2].name = "";
|
||||
cart[2].qty = 0;
|
||||
cart[3].name = "";
|
||||
cart[3].qty = 0;
|
||||
cart[4].name = "";
|
||||
cart[4].qty = 0;
|
||||
console.log(cart);
|
||||
window.alert("Cart has been cleared!");
|
||||
}
|
||||
|
||||
function calulateFinalPriceWithDiscount() {
|
||||
var subtotal = 0;
|
||||
var tax = 0;
|
||||
var total = 0;
|
||||
// for (var i = 0; i <= cart.length; i++) {
|
||||
// subtotal += cart[i].price;
|
||||
// }
|
||||
subtotal += cart[0].price;
|
||||
subtotal += cart[1].price;
|
||||
subtotal += cart[2].price;
|
||||
subtotal += cart[3].price;
|
||||
subtotal += cart[4].price;
|
||||
tax = subtotal * TAX_RATE;
|
||||
discount = subtotal * DISCOUNT_RATE;
|
||||
total = subtotal - discount + tax;
|
||||
window.alert("Subtotal: $" + subtotal + " Tax: $" + tax + " Total: $" + total);
|
||||
}
|
||||
|
||||
var notDone = true;
|
||||
while(notDone === true) {
|
||||
var userChoice = window.prompt("Please make a selection (s)earch, (u)pdate, (c)lear, (d)iscount: ").toLowerCase();
|
||||
if (userChoice === "s") {
|
||||
searchItem();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "u") {
|
||||
updateItemQuantity();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "c") {
|
||||
clearCart();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "d") {
|
||||
calulateFinalPriceWithDiscount();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else {
|
||||
window.alert("Invalid selection, try again.");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Levi McLean Midterm Part B</title>
|
||||
<meta charset="utf-8" lang="en-us">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Levi McLean Midterm Part B</h1>
|
||||
<script>
|
||||
const TAX_RATE = 0.13;
|
||||
const DISCOUNT_RATE = 0.1;
|
||||
|
||||
var item1 = {
|
||||
name: "apple",
|
||||
price: 1.1,
|
||||
qty: 4
|
||||
};
|
||||
|
||||
var item2 = {
|
||||
name: "banana",
|
||||
price: 0.59,
|
||||
qty: 2
|
||||
};
|
||||
|
||||
var item3 = {
|
||||
name: "pear",
|
||||
price: 1.25,
|
||||
qty: 6
|
||||
};
|
||||
|
||||
var item4 = {
|
||||
name: "grapes",
|
||||
price: 2.25,
|
||||
qty: 8
|
||||
};
|
||||
|
||||
var item5 = {
|
||||
name: "kiwi",
|
||||
price: 3,
|
||||
qty: 1
|
||||
};
|
||||
|
||||
var cart = [item1, item2, item3, item4, item5];
|
||||
|
||||
function searchItem() {
|
||||
var itemName = window.prompt("Enter an item name: ").toLowerCase();
|
||||
var foundItem;
|
||||
|
||||
if (itemName == null) {
|
||||
window.alert("Item name cannot be null.");
|
||||
}
|
||||
|
||||
for (var i=0; i <= cart.length; i++) {
|
||||
if (cart[i].name === itemName) {
|
||||
window.alert("Item(s) found.");
|
||||
foundItem = cart[i];
|
||||
return foundItem;
|
||||
}
|
||||
}
|
||||
window.alert("Item(s) not found.");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function updateItemQuantity() {
|
||||
var updateItem = window.prompt("Enter item name to update: ").toLowerCase();
|
||||
|
||||
if (updateItem == null) {
|
||||
window.alert("Invalid item name.");
|
||||
}
|
||||
|
||||
for (var i=0; i <= cart.length; i++) {
|
||||
if (cart[i].name === updateItem) {
|
||||
var updateQty = parseInt(window.prompt("How many items do we add? "), 10);
|
||||
cart[i].qty += updateQty;
|
||||
console.log(cart);
|
||||
return updateItem;
|
||||
}
|
||||
window.alert("Item does not exist in cart.");
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function clearCart() {
|
||||
cart[0].name = "";
|
||||
cart[0].qty = 0;
|
||||
cart[1].name = "";
|
||||
cart[1].qty = 0;
|
||||
cart[2].name = "";
|
||||
cart[2].qty = 0;
|
||||
cart[3].name = "";
|
||||
cart[3].qty = 0;
|
||||
cart[4].name = "";
|
||||
cart[4].qty = 0;
|
||||
console.log(cart);
|
||||
window.alert("Cart has been cleared!");
|
||||
}
|
||||
|
||||
function calulateFinalPriceWithDiscount() {
|
||||
var subtotal = 0;
|
||||
var tax = 0;
|
||||
var total = 0;
|
||||
// for (var i = 0; i <= cart.length; i++) {
|
||||
// subtotal += cart[i].price;
|
||||
// }
|
||||
subtotal += cart[0].price;
|
||||
subtotal += cart[1].price;
|
||||
subtotal += cart[2].price;
|
||||
subtotal += cart[3].price;
|
||||
subtotal += cart[4].price;
|
||||
tax = subtotal * TAX_RATE;
|
||||
discount = subtotal * DISCOUNT_RATE;
|
||||
total = subtotal - discount + tax;
|
||||
window.alert("Subtotal: $" + subtotal + " Tax: $" + tax + " Total: $" + total);
|
||||
}
|
||||
|
||||
var notDone = true;
|
||||
while(notDone === true) {
|
||||
var userChoice = window.prompt("Please make a selection (s)earch, (u)pdate, (c)lear, (d)iscount: ").toLowerCase();
|
||||
if (userChoice === "s") {
|
||||
searchItem();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "u") {
|
||||
updateItemQuantity();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "c") {
|
||||
clearCart();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else if (userChoice === "d") {
|
||||
calulateFinalPriceWithDiscount();
|
||||
notDone = window.confirm("Do you wish to continue?");
|
||||
} else {
|
||||
window.alert("Invalid selection, try again.");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,176 +1,176 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Midterm Study</title>
|
||||
<meta charset="utf-8" lang="en-us">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Midterm Study</h1>
|
||||
<h2>Introduction to Web Pages</h2>
|
||||
<ol>
|
||||
<li>A web page is a container for the following three technologies. Describe the purpose of each for a web page:
|
||||
</li>
|
||||
<ul>
|
||||
<li>HTML</li>
|
||||
<li>CSS</li>
|
||||
<li>JavaScript</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>HTML is used for defining web elements and the rough layout of a website</li>
|
||||
<li>CSS is used to style the default HTML elements</li>
|
||||
<li>JavaScript is used for data input, manipulation and other logical components</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>Introduction to javascript</h2>
|
||||
<ol>
|
||||
<li>Browser pop-up dialogue box functions</li>
|
||||
<ul>
|
||||
<li>alert()</li>
|
||||
<li>prompt()</li>
|
||||
<li>confirm()</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>Reload webpage to see examples</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>Data Types and Variables</h2>
|
||||
<ol>
|
||||
<li>Name JavaScript's three data types. Describe the types of data they store</li>
|
||||
<li>JavaScript is a weakly-typed language. Describe what this means.</li>
|
||||
<li>What are the naming rules for a variables name? What are naming conventions?</li>
|
||||
<li>What is a constant in JavaScript? Describe how you would declare a JavaScript constant.</li>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ol>
|
||||
<li>Number, String and Boolean</li>
|
||||
<li>weakly-typed language implies that data types do not need to be defined at initialization, the compiler will
|
||||
decide, cast and convert the data type unless explicitly cast</li>
|
||||
<li>A naming convention is a recommended way of naming variables to ensure readability and organize code.
|
||||
JavaScript uses camelCase for variables and classes and UPPER_SNAKE_CASE for constants</li>
|
||||
<code>
|
||||
let customerAge = 23;<br>
|
||||
const ONT_TAX_RATE = 0.13;
|
||||
</code>
|
||||
<li>A constant is a variable whose value cannot be changed once initialized, making it constant. To define a
|
||||
constant, use the "const" keyword.</li>
|
||||
</ol>
|
||||
<hr>
|
||||
|
||||
<h2>Operators</h2>
|
||||
<ol>
|
||||
<li>Explain the following binary operators: + - * / %</li>
|
||||
<li>Explain the multiple uses of the + operator.</li>
|
||||
<li>Describe how the increment/decrement operators (++ and --) work.</li>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ol>
|
||||
<li>The + operator is used to get the sum, the - operator is to get the difference, the * operator multiplies,
|
||||
the / operator divides and the % (modulus) operator determines if two numbers divided have a remainder and
|
||||
how much of one</li>
|
||||
<li>The + operator can be used to add two numbers together to find their sum, or you can use it to concatenate
|
||||
multiple Strings together.</li>
|
||||
<li>The ++ (increment) operator is used to increase the value of a variable by exactly one. The -- (decrement)
|
||||
operator is used to decrease the value of a variable by exactly 1.</li>
|
||||
</ol>
|
||||
<hr>
|
||||
|
||||
<h2>The JavaScript Math object</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following Math methods:</li>
|
||||
<ul>
|
||||
<li>Math.PI</li>
|
||||
<li>Math.abs(x)</li>
|
||||
<li>Math.pow(x,2)</li>
|
||||
<li>Math.round(x)</li>
|
||||
<li>Math.ceil(x)</li>
|
||||
<li>Math.floor(x)</li>
|
||||
<li>Math.trunc(x)</li>
|
||||
<li>Math.random()</li>
|
||||
<li>Math.min(x,y)</li>
|
||||
<li>Math.max(x,y)</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>Math.PI is a constant value for PI</li>
|
||||
<li>Math.abs(x) returns the absolute value of X (makes a negative positive)</li>
|
||||
<li>Math.pow(x,2) returns the value of x to the power of y (in this case 2)</li>
|
||||
<li>Math.round(x) returns x rounded to the nearest whole. Below 0.5 is rounded down, above 0.5 is rouded up.
|
||||
</li>
|
||||
<li>Math.ceil(x) returns the "ceiling" of x, which is the next highest whole integer.</li>
|
||||
<li>Math.floor(x) returns the "floor" of x, which is the next lowest whole integer.</li>
|
||||
<li>Math.trunc(x) returns x truncated, which means it cuts off the decimal places regardless of what they are.
|
||||
</li>
|
||||
<li>Math.random() returns a random number between 0 and 1, can be manipulated to return in between any range
|
||||
</li>
|
||||
<li>Math.min(x,y) returns the minimum of two numbers. The smaller of the two</li>
|
||||
<li>Math.max(x,y) returns the maximum of two numbers. The larger of the two</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>JavaScript Number Methods</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following Number methods:</li>
|
||||
<ul>
|
||||
<li>toFixed()</li>
|
||||
<li>toPrecision()</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>toFixed() returns x but at a fixed number of decimal places, defined by y</li>
|
||||
<li>toPrecision() returns x but at a fixed number of significant digits. This means it may round deicimal places
|
||||
before dropping them.</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>JavaScript Global Functions</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following global functions:</li>
|
||||
<ul>
|
||||
<li>parseInt(number)</li>
|
||||
<li>parseFloat(number)</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>parseInt(number) takes a string as input and returns the first integer in the string. Can also accept a
|
||||
radix option</li>
|
||||
<li>parseFloat(number) takes a string as input and returns the first float value in the string.</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>JavaScript String Methods</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following String methods.</li>
|
||||
<ul>
|
||||
<li>length</li>
|
||||
<li>toLowerCase()</li>
|
||||
<li>toUpperCase()</li>
|
||||
<li>charAt()</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>length gets the character count of a string including spaces</li>
|
||||
<li>toLowerCase() turns all uppercase letters to lowercase letters</li>
|
||||
<li>toUpperCase() turns all lowercase letters to uppercase letters</li>
|
||||
<li>chatAt() takes a number for the index of the word to search through and returns the character at that index.
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h2>Playground</h2>
|
||||
<p>Interactive JavaScript follows</p>
|
||||
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Midterm Study</title>
|
||||
<meta charset="utf-8" lang="en-us">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Midterm Study</h1>
|
||||
<h2>Introduction to Web Pages</h2>
|
||||
<ol>
|
||||
<li>A web page is a container for the following three technologies. Describe the purpose of each for a web page:
|
||||
</li>
|
||||
<ul>
|
||||
<li>HTML</li>
|
||||
<li>CSS</li>
|
||||
<li>JavaScript</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>HTML is used for defining web elements and the rough layout of a website</li>
|
||||
<li>CSS is used to style the default HTML elements</li>
|
||||
<li>JavaScript is used for data input, manipulation and other logical components</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>Introduction to javascript</h2>
|
||||
<ol>
|
||||
<li>Browser pop-up dialogue box functions</li>
|
||||
<ul>
|
||||
<li>alert()</li>
|
||||
<li>prompt()</li>
|
||||
<li>confirm()</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>Reload webpage to see examples</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>Data Types and Variables</h2>
|
||||
<ol>
|
||||
<li>Name JavaScript's three data types. Describe the types of data they store</li>
|
||||
<li>JavaScript is a weakly-typed language. Describe what this means.</li>
|
||||
<li>What are the naming rules for a variables name? What are naming conventions?</li>
|
||||
<li>What is a constant in JavaScript? Describe how you would declare a JavaScript constant.</li>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ol>
|
||||
<li>Number, String and Boolean</li>
|
||||
<li>weakly-typed language implies that data types do not need to be defined at initialization, the compiler will
|
||||
decide, cast and convert the data type unless explicitly cast</li>
|
||||
<li>A naming convention is a recommended way of naming variables to ensure readability and organize code.
|
||||
JavaScript uses camelCase for variables and classes and UPPER_SNAKE_CASE for constants</li>
|
||||
<code>
|
||||
let customerAge = 23;<br>
|
||||
const ONT_TAX_RATE = 0.13;
|
||||
</code>
|
||||
<li>A constant is a variable whose value cannot be changed once initialized, making it constant. To define a
|
||||
constant, use the "const" keyword.</li>
|
||||
</ol>
|
||||
<hr>
|
||||
|
||||
<h2>Operators</h2>
|
||||
<ol>
|
||||
<li>Explain the following binary operators: + - * / %</li>
|
||||
<li>Explain the multiple uses of the + operator.</li>
|
||||
<li>Describe how the increment/decrement operators (++ and --) work.</li>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ol>
|
||||
<li>The + operator is used to get the sum, the - operator is to get the difference, the * operator multiplies,
|
||||
the / operator divides and the % (modulus) operator determines if two numbers divided have a remainder and
|
||||
how much of one</li>
|
||||
<li>The + operator can be used to add two numbers together to find their sum, or you can use it to concatenate
|
||||
multiple Strings together.</li>
|
||||
<li>The ++ (increment) operator is used to increase the value of a variable by exactly one. The -- (decrement)
|
||||
operator is used to decrease the value of a variable by exactly 1.</li>
|
||||
</ol>
|
||||
<hr>
|
||||
|
||||
<h2>The JavaScript Math object</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following Math methods:</li>
|
||||
<ul>
|
||||
<li>Math.PI</li>
|
||||
<li>Math.abs(x)</li>
|
||||
<li>Math.pow(x,2)</li>
|
||||
<li>Math.round(x)</li>
|
||||
<li>Math.ceil(x)</li>
|
||||
<li>Math.floor(x)</li>
|
||||
<li>Math.trunc(x)</li>
|
||||
<li>Math.random()</li>
|
||||
<li>Math.min(x,y)</li>
|
||||
<li>Math.max(x,y)</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>Math.PI is a constant value for PI</li>
|
||||
<li>Math.abs(x) returns the absolute value of X (makes a negative positive)</li>
|
||||
<li>Math.pow(x,2) returns the value of x to the power of y (in this case 2)</li>
|
||||
<li>Math.round(x) returns x rounded to the nearest whole. Below 0.5 is rounded down, above 0.5 is rouded up.
|
||||
</li>
|
||||
<li>Math.ceil(x) returns the "ceiling" of x, which is the next highest whole integer.</li>
|
||||
<li>Math.floor(x) returns the "floor" of x, which is the next lowest whole integer.</li>
|
||||
<li>Math.trunc(x) returns x truncated, which means it cuts off the decimal places regardless of what they are.
|
||||
</li>
|
||||
<li>Math.random() returns a random number between 0 and 1, can be manipulated to return in between any range
|
||||
</li>
|
||||
<li>Math.min(x,y) returns the minimum of two numbers. The smaller of the two</li>
|
||||
<li>Math.max(x,y) returns the maximum of two numbers. The larger of the two</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>JavaScript Number Methods</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following Number methods:</li>
|
||||
<ul>
|
||||
<li>toFixed()</li>
|
||||
<li>toPrecision()</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>toFixed() returns x but at a fixed number of decimal places, defined by y</li>
|
||||
<li>toPrecision() returns x but at a fixed number of significant digits. This means it may round deicimal places
|
||||
before dropping them.</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>JavaScript Global Functions</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following global functions:</li>
|
||||
<ul>
|
||||
<li>parseInt(number)</li>
|
||||
<li>parseFloat(number)</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>parseInt(number) takes a string as input and returns the first integer in the string. Can also accept a
|
||||
radix option</li>
|
||||
<li>parseFloat(number) takes a string as input and returns the first float value in the string.</li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<h2>JavaScript String Methods</h2>
|
||||
<ol>
|
||||
<li>Describe the operation and the return for the following String methods.</li>
|
||||
<ul>
|
||||
<li>length</li>
|
||||
<li>toLowerCase()</li>
|
||||
<li>toUpperCase()</li>
|
||||
<li>charAt()</li>
|
||||
</ul>
|
||||
</ol>
|
||||
<p>Answers:</p>
|
||||
<ul>
|
||||
<li>length gets the character count of a string including spaces</li>
|
||||
<li>toLowerCase() turns all uppercase letters to lowercase letters</li>
|
||||
<li>toUpperCase() turns all lowercase letters to uppercase letters</li>
|
||||
<li>chatAt() takes a number for the index of the word to search through and returns the character at that index.
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h2>Playground</h2>
|
||||
<p>Interactive JavaScript follows</p>
|
||||
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,190 +1,190 @@
|
||||
// for (var i = 1; i < 10; i++) {
|
||||
// console.log("Ran " + i + " time(s).")
|
||||
// };
|
||||
|
||||
// console.log(window.confirm("Feel prepared?")); // true or false
|
||||
|
||||
// var question = ("50" === 50);
|
||||
// console.log(question);
|
||||
|
||||
// var question2 = (true && false);
|
||||
// console.log(question2);
|
||||
|
||||
// var question3 = (true || false);
|
||||
// console.log(question3);
|
||||
|
||||
// // VAR price = 35.6; <-- WRONG
|
||||
// var price = 35.6; // <-- RIGHT
|
||||
|
||||
// var city = "London";
|
||||
|
||||
// if (city.toLowerCase() === "london") {
|
||||
// console.log("I live there too!");
|
||||
// } else {
|
||||
// console.log("I dont know where that is.")
|
||||
// }
|
||||
|
||||
// var userName = window.prompt("Please enter your name:");
|
||||
// greetUser(userName);
|
||||
|
||||
// var radius = window.prompt("Please enter circle radius:");
|
||||
// calcCirc(radius);
|
||||
|
||||
// var testNum = window.prompt("Number to test for even:");
|
||||
// isEven(testNum);
|
||||
|
||||
// function greetUser(userName) {
|
||||
// alert("Hello " + userName + " welcome to the page");
|
||||
// }
|
||||
|
||||
// function calcCirc(radius) {
|
||||
// var Circ = ((2 * Math.PI) * radius).toFixed(2);
|
||||
// alert("Circumference is: " + Circ + " Inches.");
|
||||
// }
|
||||
|
||||
// function isEven(number) {
|
||||
// if (number % 2 == 0) {
|
||||
// console.log(number + " is even");
|
||||
// } else {
|
||||
// console.log(number + " is odd");
|
||||
// }
|
||||
// }
|
||||
|
||||
// NUMBER GUESSING GAME
|
||||
// var randomNum = Math.floor((Math.random() * 100) + 1);
|
||||
// console.log("Random Number: " + randomNum);
|
||||
|
||||
// var userGuess;
|
||||
// do {
|
||||
// var input = window.prompt("Guess a number from 1-100: ");
|
||||
// if (input === null) {
|
||||
// window.alert("Game cancelled.");
|
||||
// break;
|
||||
// }
|
||||
|
||||
// userGuess = parseInt(input, 10);
|
||||
// if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
|
||||
// window.alert("Please enter a valid number between 1 and 100.");
|
||||
// } else if (userGuess !== randomNum) {
|
||||
// window.alert("Incorrect number, try again!");
|
||||
// }
|
||||
// } while (userGuess !== randomNum);
|
||||
|
||||
// if (userGuess === randomNum) {
|
||||
// window.alert("You guessed the right number! The number was " + randomNum);
|
||||
// }
|
||||
|
||||
// ROCK PAPER SCISSORS GAME
|
||||
// var choices = ["rock", "paper", "scissors"];
|
||||
// var compChoice = choices[Math.floor(Math.random() * 3)];
|
||||
// console.log("Computer choice: " + compChoice);
|
||||
|
||||
// var userChoice = window.prompt("Enter rock, paper or scissors: ").toLowerCase();
|
||||
// console.log("User choice: " + userChoice);
|
||||
|
||||
// if (!choices.includes(userChoice)) {
|
||||
// window.alert("Invalid choice!");
|
||||
// } else if (userChoice === compChoice) {
|
||||
// window.alert("It's a tie!");
|
||||
// } else {
|
||||
// var winsAgainst = {
|
||||
// rock: "scissors",
|
||||
// paper: "rock",
|
||||
// scissors: "paper"
|
||||
// };
|
||||
|
||||
// if (winsAgainst[userChoice] === compChoice) {
|
||||
// window.alert("You win!");
|
||||
// } else {
|
||||
// window.alert("Computer wins!");
|
||||
// }
|
||||
// }
|
||||
|
||||
// SAMPLE BANK ACCOUNT
|
||||
// var bankBalance = 0;
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
|
||||
// function getAmount(message) {
|
||||
// var input = window.prompt(message);
|
||||
// if (input === null) return null;
|
||||
// var amount = parseInt(input, 10);
|
||||
// if (isNaN(amount)) {
|
||||
// window.alert("Please enter a valid number.");
|
||||
// return null;
|
||||
// }
|
||||
// return amount;
|
||||
// }
|
||||
|
||||
// while (true) {
|
||||
// var userChoice = window.prompt(
|
||||
// "Welcome! Your bank balance is: $" + bankBalance +
|
||||
// ". Would you like to (w)ithdraw, (d)eposit or (v)iew balance, or (q)uit?");
|
||||
// if (userChoice === null || userChoice.toLowerCase() === "q") break;
|
||||
// userChoice = userChoice.toLowerCase();
|
||||
// console.log(userChoice);
|
||||
|
||||
// if (userChoice === "w" || userChoice === "withdrawl") {
|
||||
// var widthdrawlAmount = getAmount("How much would you like to widthdraw?");
|
||||
// if (widthdrawlAmount === null) continue;
|
||||
// if (widthdrawlAmount > bankBalance) {
|
||||
// window.alert("Not enough in balance to widthdraw, sorry!");
|
||||
// } else if (widthdrawlAmount <= 0) {
|
||||
// window.alert("Cannot widthdraw negative or 0 dollars.");
|
||||
// } else {
|
||||
// bankBalance -= widthdrawlAmount;
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
// }
|
||||
// } else if (userChoice === "d" || userChoice === "deposit") {
|
||||
// var depositAmount = getAmount("How much would you like to deposit?");
|
||||
// if (depositAmount === null) continue;
|
||||
// if (depositAmount <= 0) {
|
||||
// window.alert("Cannot deposit negative or 0 dollars.");
|
||||
// } else {
|
||||
// bankBalance += depositAmount;
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
// }
|
||||
// } else if (userChoice === "v" || userChoice === "view") {
|
||||
// window.alert("Your balance is: $" + bankBalance + " dollars. Thank you for banking with us!");
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
// } else {
|
||||
// window.alert("Invalid choice, please try again");
|
||||
// }
|
||||
// }
|
||||
|
||||
// ARRAY STATS CALCULATOR
|
||||
// var stats = [];
|
||||
// var sum = 0;
|
||||
|
||||
// for (var i = 0; i < 50; i++) {
|
||||
// var randomNum = Math.floor(Math.random() * 50);
|
||||
// sum += randomNum;
|
||||
// stats.push(randomNum);
|
||||
// console.log(randomNum);
|
||||
// }
|
||||
// console.log(stats);
|
||||
|
||||
// console.log("Sum of stats: " + sum);
|
||||
// console.log("Average of stats: " + (sum / stats.length));
|
||||
// console.log("Min of stats: " + Math.min(...stats));
|
||||
// console.log("Max of stats: " + Math.max(...stats));
|
||||
|
||||
// PASSWORD STRENGTH CHECKER
|
||||
var userPass = window.prompt("Enter a password to test: ");
|
||||
var charCount, numCount, symCount;
|
||||
|
||||
charCount = userPass.length;
|
||||
console.log(charCount);
|
||||
|
||||
numCount = (userPass.match(/\d/g) || []).length;
|
||||
console.log(numCount);
|
||||
|
||||
symCount = (userPass.match(/[^a-zA-Z0-9]/g) || []).length;
|
||||
console.log(symCount);
|
||||
|
||||
if (charCount >= 10 && numCount >= 3 && symCount >= 1) {
|
||||
window.alert("Your password is STRONG");
|
||||
} else if (charCount >= 10 && numCount >= 1) {
|
||||
window.alert("Your password is OKAY");
|
||||
} else {
|
||||
window.alert("Your password is WEAK");
|
||||
// for (var i = 1; i < 10; i++) {
|
||||
// console.log("Ran " + i + " time(s).")
|
||||
// };
|
||||
|
||||
// console.log(window.confirm("Feel prepared?")); // true or false
|
||||
|
||||
// var question = ("50" === 50);
|
||||
// console.log(question);
|
||||
|
||||
// var question2 = (true && false);
|
||||
// console.log(question2);
|
||||
|
||||
// var question3 = (true || false);
|
||||
// console.log(question3);
|
||||
|
||||
// // VAR price = 35.6; <-- WRONG
|
||||
// var price = 35.6; // <-- RIGHT
|
||||
|
||||
// var city = "London";
|
||||
|
||||
// if (city.toLowerCase() === "london") {
|
||||
// console.log("I live there too!");
|
||||
// } else {
|
||||
// console.log("I dont know where that is.")
|
||||
// }
|
||||
|
||||
// var userName = window.prompt("Please enter your name:");
|
||||
// greetUser(userName);
|
||||
|
||||
// var radius = window.prompt("Please enter circle radius:");
|
||||
// calcCirc(radius);
|
||||
|
||||
// var testNum = window.prompt("Number to test for even:");
|
||||
// isEven(testNum);
|
||||
|
||||
// function greetUser(userName) {
|
||||
// alert("Hello " + userName + " welcome to the page");
|
||||
// }
|
||||
|
||||
// function calcCirc(radius) {
|
||||
// var Circ = ((2 * Math.PI) * radius).toFixed(2);
|
||||
// alert("Circumference is: " + Circ + " Inches.");
|
||||
// }
|
||||
|
||||
// function isEven(number) {
|
||||
// if (number % 2 == 0) {
|
||||
// console.log(number + " is even");
|
||||
// } else {
|
||||
// console.log(number + " is odd");
|
||||
// }
|
||||
// }
|
||||
|
||||
// NUMBER GUESSING GAME
|
||||
// var randomNum = Math.floor((Math.random() * 100) + 1);
|
||||
// console.log("Random Number: " + randomNum);
|
||||
|
||||
// var userGuess;
|
||||
// do {
|
||||
// var input = window.prompt("Guess a number from 1-100: ");
|
||||
// if (input === null) {
|
||||
// window.alert("Game cancelled.");
|
||||
// break;
|
||||
// }
|
||||
|
||||
// userGuess = parseInt(input, 10);
|
||||
// if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
|
||||
// window.alert("Please enter a valid number between 1 and 100.");
|
||||
// } else if (userGuess !== randomNum) {
|
||||
// window.alert("Incorrect number, try again!");
|
||||
// }
|
||||
// } while (userGuess !== randomNum);
|
||||
|
||||
// if (userGuess === randomNum) {
|
||||
// window.alert("You guessed the right number! The number was " + randomNum);
|
||||
// }
|
||||
|
||||
// ROCK PAPER SCISSORS GAME
|
||||
// var choices = ["rock", "paper", "scissors"];
|
||||
// var compChoice = choices[Math.floor(Math.random() * 3)];
|
||||
// console.log("Computer choice: " + compChoice);
|
||||
|
||||
// var userChoice = window.prompt("Enter rock, paper or scissors: ").toLowerCase();
|
||||
// console.log("User choice: " + userChoice);
|
||||
|
||||
// if (!choices.includes(userChoice)) {
|
||||
// window.alert("Invalid choice!");
|
||||
// } else if (userChoice === compChoice) {
|
||||
// window.alert("It's a tie!");
|
||||
// } else {
|
||||
// var winsAgainst = {
|
||||
// rock: "scissors",
|
||||
// paper: "rock",
|
||||
// scissors: "paper"
|
||||
// };
|
||||
|
||||
// if (winsAgainst[userChoice] === compChoice) {
|
||||
// window.alert("You win!");
|
||||
// } else {
|
||||
// window.alert("Computer wins!");
|
||||
// }
|
||||
// }
|
||||
|
||||
// SAMPLE BANK ACCOUNT
|
||||
// var bankBalance = 0;
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
|
||||
// function getAmount(message) {
|
||||
// var input = window.prompt(message);
|
||||
// if (input === null) return null;
|
||||
// var amount = parseInt(input, 10);
|
||||
// if (isNaN(amount)) {
|
||||
// window.alert("Please enter a valid number.");
|
||||
// return null;
|
||||
// }
|
||||
// return amount;
|
||||
// }
|
||||
|
||||
// while (true) {
|
||||
// var userChoice = window.prompt(
|
||||
// "Welcome! Your bank balance is: $" + bankBalance +
|
||||
// ". Would you like to (w)ithdraw, (d)eposit or (v)iew balance, or (q)uit?");
|
||||
// if (userChoice === null || userChoice.toLowerCase() === "q") break;
|
||||
// userChoice = userChoice.toLowerCase();
|
||||
// console.log(userChoice);
|
||||
|
||||
// if (userChoice === "w" || userChoice === "withdrawl") {
|
||||
// var widthdrawlAmount = getAmount("How much would you like to widthdraw?");
|
||||
// if (widthdrawlAmount === null) continue;
|
||||
// if (widthdrawlAmount > bankBalance) {
|
||||
// window.alert("Not enough in balance to widthdraw, sorry!");
|
||||
// } else if (widthdrawlAmount <= 0) {
|
||||
// window.alert("Cannot widthdraw negative or 0 dollars.");
|
||||
// } else {
|
||||
// bankBalance -= widthdrawlAmount;
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
// }
|
||||
// } else if (userChoice === "d" || userChoice === "deposit") {
|
||||
// var depositAmount = getAmount("How much would you like to deposit?");
|
||||
// if (depositAmount === null) continue;
|
||||
// if (depositAmount <= 0) {
|
||||
// window.alert("Cannot deposit negative or 0 dollars.");
|
||||
// } else {
|
||||
// bankBalance += depositAmount;
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
// }
|
||||
// } else if (userChoice === "v" || userChoice === "view") {
|
||||
// window.alert("Your balance is: $" + bankBalance + " dollars. Thank you for banking with us!");
|
||||
// console.log("Bank balance: $" + bankBalance);
|
||||
// } else {
|
||||
// window.alert("Invalid choice, please try again");
|
||||
// }
|
||||
// }
|
||||
|
||||
// ARRAY STATS CALCULATOR
|
||||
// var stats = [];
|
||||
// var sum = 0;
|
||||
|
||||
// for (var i = 0; i < 50; i++) {
|
||||
// var randomNum = Math.floor(Math.random() * 50);
|
||||
// sum += randomNum;
|
||||
// stats.push(randomNum);
|
||||
// console.log(randomNum);
|
||||
// }
|
||||
// console.log(stats);
|
||||
|
||||
// console.log("Sum of stats: " + sum);
|
||||
// console.log("Average of stats: " + (sum / stats.length));
|
||||
// console.log("Min of stats: " + Math.min(...stats));
|
||||
// console.log("Max of stats: " + Math.max(...stats));
|
||||
|
||||
// PASSWORD STRENGTH CHECKER
|
||||
var userPass = window.prompt("Enter a password to test: ");
|
||||
var charCount, numCount, symCount;
|
||||
|
||||
charCount = userPass.length;
|
||||
console.log(charCount);
|
||||
|
||||
numCount = (userPass.match(/\d/g) || []).length;
|
||||
console.log(numCount);
|
||||
|
||||
symCount = (userPass.match(/[^a-zA-Z0-9]/g) || []).length;
|
||||
console.log(symCount);
|
||||
|
||||
if (charCount >= 10 && numCount >= 3 && symCount >= 1) {
|
||||
window.alert("Your password is STRONG");
|
||||
} else if (charCount >= 10 && numCount >= 1) {
|
||||
window.alert("Your password is OKAY");
|
||||
} else {
|
||||
window.alert("Your password is WEAK");
|
||||
}
|
||||
@@ -1,34 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 1 Class 1</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Javascript test</h1>
|
||||
<script>
|
||||
document.write("Hello Class, Welcome to JavaScript <br>");
|
||||
document.write("I love JavaScript");
|
||||
|
||||
document.write("Hello Class, Welcome to JavaScript")
|
||||
document.write("<br>");
|
||||
document.write("I love JavaScript <br>");
|
||||
|
||||
window.alert("ALERT ALERT ALERT ALERT");
|
||||
window.confirm("CONFIRM PLEASE CONFIRM PLEASE");
|
||||
window.prompt("PROMPT HERE PROMPT HERE");
|
||||
</script>
|
||||
<button type="submit" id="testButton" onclick="testButton()">
|
||||
<p>Press me!</p>
|
||||
</button>
|
||||
<p id="buttonLabel">Button pressed!</p>
|
||||
<hr>
|
||||
<h2>Form example</h2>
|
||||
<input type="text" id="firstName" placeholder="First name">
|
||||
<input type="text" id="lastName" placeholder="Last name">
|
||||
<input type="email" id="email" placeholder="Email">
|
||||
<input type="text" id="phone" placeholder="Phone number">
|
||||
<p id="formLabel">You entered: </p>
|
||||
<button type="submit" id="repeatButton" onclick="repeatInfo()">Repeat Info</button>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 1 Class 1</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Javascript test</h1>
|
||||
<script>
|
||||
document.write("Hello Class, Welcome to JavaScript <br>");
|
||||
document.write("I love JavaScript");
|
||||
|
||||
document.write("Hello Class, Welcome to JavaScript")
|
||||
document.write("<br>");
|
||||
document.write("I love JavaScript <br>");
|
||||
|
||||
window.alert("ALERT ALERT ALERT ALERT");
|
||||
window.confirm("CONFIRM PLEASE CONFIRM PLEASE");
|
||||
window.prompt("PROMPT HERE PROMPT HERE");
|
||||
</script>
|
||||
<button type="submit" id="testButton" onclick="testButton()">
|
||||
<p>Press me!</p>
|
||||
</button>
|
||||
<p id="buttonLabel">Button pressed!</p>
|
||||
<hr>
|
||||
<h2>Form example</h2>
|
||||
<input type="text" id="firstName" placeholder="First name">
|
||||
<input type="text" id="lastName" placeholder="Last name">
|
||||
<input type="email" id="email" placeholder="Email">
|
||||
<input type="text" id="phone" placeholder="Phone number">
|
||||
<p id="formLabel">You entered: </p>
|
||||
<button type="submit" id="repeatButton" onclick="repeatInfo()">Repeat Info</button>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,19 +1,19 @@
|
||||
document.getElementById("buttonLabel").style.display = "none";
|
||||
|
||||
function testButton() {
|
||||
const buttonLabel = document.getElementById("buttonLabel");
|
||||
if (buttonLabel.style.display === "block") {
|
||||
buttonLabel.style.display = "none";
|
||||
} else {
|
||||
buttonLabel.style.display = "block";
|
||||
}
|
||||
}
|
||||
|
||||
function repeatInfo() {
|
||||
const formLabel = document.getElementById("formLabel");
|
||||
const firstName = document.getElementById("firstName").value;
|
||||
const lastName = document.getElementById("lastName").value;
|
||||
const email = document.getElementById("email").value;
|
||||
const phone = Number(document.getElementById("phone").value);
|
||||
formLabel.innerHTML += `${firstName} ${lastName}, ${email}, ${phone}`
|
||||
document.getElementById("buttonLabel").style.display = "none";
|
||||
|
||||
function testButton() {
|
||||
const buttonLabel = document.getElementById("buttonLabel");
|
||||
if (buttonLabel.style.display === "block") {
|
||||
buttonLabel.style.display = "none";
|
||||
} else {
|
||||
buttonLabel.style.display = "block";
|
||||
}
|
||||
}
|
||||
|
||||
function repeatInfo() {
|
||||
const formLabel = document.getElementById("formLabel");
|
||||
const firstName = document.getElementById("firstName").value;
|
||||
const lastName = document.getElementById("lastName").value;
|
||||
const email = document.getElementById("email").value;
|
||||
const phone = Number(document.getElementById("phone").value);
|
||||
formLabel.innerHTML += `${firstName} ${lastName}, ${email}, ${phone}`
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 3 Class 1</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Week 3 Class 1</h1>
|
||||
<p>Enter some details</p>
|
||||
<input id="firstNameBox" type="text" placeholder="First Name">
|
||||
<input id="lastNameBox" type="text" placeholder="Last Name">
|
||||
<input id="ageBox" type="number" placeholder="Age">
|
||||
<input id="passwordBox" type="password" placeholder="Password">
|
||||
<button type="submit" onclick="showDetails()">Submit</button>
|
||||
<p id="detailsLabel">Entered Details: </p>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 3 Class 1</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Week 3 Class 1</h1>
|
||||
<p>Enter some details</p>
|
||||
<input id="firstNameBox" type="text" placeholder="First Name">
|
||||
<input id="lastNameBox" type="text" placeholder="Last Name">
|
||||
<input id="ageBox" type="number" placeholder="Age">
|
||||
<input id="passwordBox" type="password" placeholder="Password">
|
||||
<button type="submit" onclick="showDetails()">Submit</button>
|
||||
<p id="detailsLabel">Entered Details: </p>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,14 +1,14 @@
|
||||
function showDetails() {
|
||||
var firstName = document.getElementById("firstNameBox").value;
|
||||
var lastName = document.getElementById("lastNameBox").value;
|
||||
var age = Number(document.getElementById("ageBox").value);
|
||||
var password = document.getElementById("passwordBox").value;
|
||||
console.log(`You entered: ${firstName}, ${lastName}, ${age}, ${password}`);
|
||||
document.getElementById("detailsLabel").innerHTML += `${firstName}, ${lastName}, ${age}, ${password}`;
|
||||
|
||||
console.log(10 + 10);
|
||||
console.log(10 * 10);
|
||||
console.log(10 - 3);
|
||||
console.log(100 / 25);
|
||||
console.log(100 % 3);
|
||||
function showDetails() {
|
||||
var firstName = document.getElementById("firstNameBox").value;
|
||||
var lastName = document.getElementById("lastNameBox").value;
|
||||
var age = Number(document.getElementById("ageBox").value);
|
||||
var password = document.getElementById("passwordBox").value;
|
||||
console.log(`You entered: ${firstName}, ${lastName}, ${age}, ${password}`);
|
||||
document.getElementById("detailsLabel").innerHTML += `${firstName}, ${lastName}, ${age}, ${password}`;
|
||||
|
||||
console.log(10 + 10);
|
||||
console.log(10 * 10);
|
||||
console.log(10 - 3);
|
||||
console.log(100 / 25);
|
||||
console.log(100 % 3);
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 8 JS Object Practice</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Week 8 JS Object Practice</h1>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Week 8 JS Object Practice</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Week 8 JS Object Practice</h1>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,63 +1,63 @@
|
||||
// Question 1
|
||||
function person(firstName, lastName, age, city) {
|
||||
return {
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
age: age,
|
||||
city: city
|
||||
};
|
||||
}
|
||||
|
||||
var testPerson = person("Levi", "McLean", 20, "Woodstock");
|
||||
console.log(testPerson.firstName);
|
||||
console.log(testPerson.lastName);
|
||||
console.log(testPerson.age);
|
||||
console.log(testPerson.city);
|
||||
|
||||
// Question 2
|
||||
function book(title, author, pages) {
|
||||
return {
|
||||
title: title,
|
||||
author: author,
|
||||
pages: pages
|
||||
};
|
||||
}
|
||||
|
||||
var testBook = book("1984", "George Orwell", 386);
|
||||
console.log(testBook.title);
|
||||
console.log(testBook.author);
|
||||
console.log(testBook.pages);
|
||||
console.log("Update pages to 555");
|
||||
testBook.pages = 555;
|
||||
console.log(testBook.pages);
|
||||
|
||||
// Question 3
|
||||
function movie(name, year, rating) {
|
||||
return {
|
||||
name: name,
|
||||
year: year,
|
||||
rating: rating
|
||||
};
|
||||
}
|
||||
|
||||
var testMovie = movie("Kill Bill", 2003, 9.5);
|
||||
console.log(testMovie.name);
|
||||
console.log(testMovie.year);
|
||||
console.log(testMovie.rating);
|
||||
console.log("Adding new property director");
|
||||
testMovie.director = "Quentin Tarantino";
|
||||
console.log(testMovie.director);
|
||||
|
||||
// Question 4
|
||||
const playlist = {
|
||||
name: "Cool Jams",
|
||||
songs: ["Rap song", "Metal song", "Pop song"]
|
||||
};
|
||||
|
||||
console.log(playlist.songs);
|
||||
console.log("First Song");
|
||||
console.log(playlist.songs[0]);
|
||||
console.log("Last Song");
|
||||
console.log(playlist.songs[playlist.songs.length - 1]); // Always get last entry
|
||||
|
||||
// Question 5
|
||||
// Question 1
|
||||
function person(firstName, lastName, age, city) {
|
||||
return {
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
age: age,
|
||||
city: city
|
||||
};
|
||||
}
|
||||
|
||||
var testPerson = person("Levi", "McLean", 20, "Woodstock");
|
||||
console.log(testPerson.firstName);
|
||||
console.log(testPerson.lastName);
|
||||
console.log(testPerson.age);
|
||||
console.log(testPerson.city);
|
||||
|
||||
// Question 2
|
||||
function book(title, author, pages) {
|
||||
return {
|
||||
title: title,
|
||||
author: author,
|
||||
pages: pages
|
||||
};
|
||||
}
|
||||
|
||||
var testBook = book("1984", "George Orwell", 386);
|
||||
console.log(testBook.title);
|
||||
console.log(testBook.author);
|
||||
console.log(testBook.pages);
|
||||
console.log("Update pages to 555");
|
||||
testBook.pages = 555;
|
||||
console.log(testBook.pages);
|
||||
|
||||
// Question 3
|
||||
function movie(name, year, rating) {
|
||||
return {
|
||||
name: name,
|
||||
year: year,
|
||||
rating: rating
|
||||
};
|
||||
}
|
||||
|
||||
var testMovie = movie("Kill Bill", 2003, 9.5);
|
||||
console.log(testMovie.name);
|
||||
console.log(testMovie.year);
|
||||
console.log(testMovie.rating);
|
||||
console.log("Adding new property director");
|
||||
testMovie.director = "Quentin Tarantino";
|
||||
console.log(testMovie.director);
|
||||
|
||||
// Question 4
|
||||
const playlist = {
|
||||
name: "Cool Jams",
|
||||
songs: ["Rap song", "Metal song", "Pop song"]
|
||||
};
|
||||
|
||||
console.log(playlist.songs);
|
||||
console.log("First Song");
|
||||
console.log(playlist.songs[0]);
|
||||
console.log("Last Song");
|
||||
console.log(playlist.songs[playlist.songs.length - 1]); // Always get last entry
|
||||
|
||||
// Question 5
|
||||
|
||||
@@ -1,116 +1,116 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Flowery Flower Shop</title>
|
||||
<meta charset="utf-8" lang="en">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Flowery Flower Shop</h1>
|
||||
<small>Browse our flowers below...</small>
|
||||
|
||||
<h2>Available flowers</h2>
|
||||
<section id="catalog">
|
||||
<article id="rose">
|
||||
<h3>Rose</h3>
|
||||
<ul>
|
||||
<li>Price:</li>
|
||||
<li>Category:</li>
|
||||
<li>Stock:</li>
|
||||
<li>Description:</li>
|
||||
</ul>
|
||||
<form class="add-to-cart">
|
||||
<label>Quantity: <input type="number" name="quantity" min="1" value="1" required></label>
|
||||
<button type="submit">Add to Cart</button>
|
||||
</form>
|
||||
</article>
|
||||
<article id="tulip">
|
||||
<h3>Tulip</h3>
|
||||
<ul>
|
||||
<li>Price:</li>
|
||||
<li>Category:</li>
|
||||
<li>Stock:</li>
|
||||
<li>Description:</li>
|
||||
</ul>
|
||||
<form class="add-to-cart">
|
||||
<label>Quantity: <input type="number" name="quantity" min="1" value="1" required></label>
|
||||
<button type="submit">Add to Cart</button>
|
||||
</form>
|
||||
</article>
|
||||
<article id="sunflower">
|
||||
<h3>Sunflower</h3>
|
||||
<ul>
|
||||
<li>Price:</li>
|
||||
<li>Category:</li>
|
||||
<li>Stock:</li>
|
||||
<li>Description:</li>
|
||||
</ul>
|
||||
<form class="add-to-cart">
|
||||
<label>Quantity: <input type="number" name="quantity" min="1" value="1" required></label>
|
||||
<button type="submit">Add to Cart</button>
|
||||
</form>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2>Shopping Cart</h2>
|
||||
<section id="cart">
|
||||
<table id="cart-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Flower</th>
|
||||
<th>Price</th>
|
||||
<th>Quantity</th>
|
||||
<th>Subtotal</th>
|
||||
<th>Remove</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
<div id="cart-total">
|
||||
<p>Subtotal: $<span id="subtotal">0.00</span></p>
|
||||
<p>Tax: $<span id="tax">0.00</span></p>
|
||||
<strong>Total: $<span id="total">0.00</span></strong>
|
||||
</div>
|
||||
<button id="checkout-button">Checkout</button>
|
||||
<div id="checkout-message"></div>
|
||||
</section>
|
||||
|
||||
<h2>Testimonials and Feedback</h2>
|
||||
<section id="feedback">
|
||||
<form id="feedback-form">
|
||||
<label>
|
||||
Flower:
|
||||
<select name="flower" required>
|
||||
<option value="">Select a flower to rate</option>
|
||||
<option value="Rose">Red Rose</option>
|
||||
<option value="Tulip">Tulip</option>
|
||||
<option value="Sunflower">Sunflower</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
Rating:
|
||||
<select name="rating" required>
|
||||
<option value="">Rate</option>
|
||||
<option value="5">5 - Excellent</option>
|
||||
<option value="4">4 - Good</option>
|
||||
<option value="3">3 - Average</option>
|
||||
<option value="2">2 - Poor</option>
|
||||
<option value="1">1 - Terrible</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
Feedback:
|
||||
<input type="text" name="content" maxlength="120" placeholder="Your feedback" required>
|
||||
</label>
|
||||
<button type="submit">Submit Feedback</button>
|
||||
</form>
|
||||
<div id="feedback-list">
|
||||
|
||||
</div>
|
||||
</section>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Flowery Flower Shop</title>
|
||||
<meta charset="utf-8" lang="en">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Flowery Flower Shop</h1>
|
||||
<small>Browse our flowers below...</small>
|
||||
|
||||
<h2>Available flowers</h2>
|
||||
<section id="catalog">
|
||||
<article id="rose">
|
||||
<h3>Rose</h3>
|
||||
<ul>
|
||||
<li>Price:</li>
|
||||
<li>Category:</li>
|
||||
<li>Stock:</li>
|
||||
<li>Description:</li>
|
||||
</ul>
|
||||
<form class="add-to-cart">
|
||||
<label>Quantity: <input type="number" name="quantity" min="1" value="1" required></label>
|
||||
<button type="submit">Add to Cart</button>
|
||||
</form>
|
||||
</article>
|
||||
<article id="tulip">
|
||||
<h3>Tulip</h3>
|
||||
<ul>
|
||||
<li>Price:</li>
|
||||
<li>Category:</li>
|
||||
<li>Stock:</li>
|
||||
<li>Description:</li>
|
||||
</ul>
|
||||
<form class="add-to-cart">
|
||||
<label>Quantity: <input type="number" name="quantity" min="1" value="1" required></label>
|
||||
<button type="submit">Add to Cart</button>
|
||||
</form>
|
||||
</article>
|
||||
<article id="sunflower">
|
||||
<h3>Sunflower</h3>
|
||||
<ul>
|
||||
<li>Price:</li>
|
||||
<li>Category:</li>
|
||||
<li>Stock:</li>
|
||||
<li>Description:</li>
|
||||
</ul>
|
||||
<form class="add-to-cart">
|
||||
<label>Quantity: <input type="number" name="quantity" min="1" value="1" required></label>
|
||||
<button type="submit">Add to Cart</button>
|
||||
</form>
|
||||
</article>
|
||||
</section>
|
||||
|
||||
<h2>Shopping Cart</h2>
|
||||
<section id="cart">
|
||||
<table id="cart-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Flower</th>
|
||||
<th>Price</th>
|
||||
<th>Quantity</th>
|
||||
<th>Subtotal</th>
|
||||
<th>Remove</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
<div id="cart-total">
|
||||
<p>Subtotal: $<span id="subtotal">0.00</span></p>
|
||||
<p>Tax: $<span id="tax">0.00</span></p>
|
||||
<strong>Total: $<span id="total">0.00</span></strong>
|
||||
</div>
|
||||
<button id="checkout-button">Checkout</button>
|
||||
<div id="checkout-message"></div>
|
||||
</section>
|
||||
|
||||
<h2>Testimonials and Feedback</h2>
|
||||
<section id="feedback">
|
||||
<form id="feedback-form">
|
||||
<label>
|
||||
Flower:
|
||||
<select name="flower" required>
|
||||
<option value="">Select a flower to rate</option>
|
||||
<option value="Rose">Red Rose</option>
|
||||
<option value="Tulip">Tulip</option>
|
||||
<option value="Sunflower">Sunflower</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
Rating:
|
||||
<select name="rating" required>
|
||||
<option value="">Rate</option>
|
||||
<option value="5">5 - Excellent</option>
|
||||
<option value="4">4 - Good</option>
|
||||
<option value="3">3 - Average</option>
|
||||
<option value="2">2 - Poor</option>
|
||||
<option value="1">1 - Terrible</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
Feedback:
|
||||
<input type="text" name="content" maxlength="120" placeholder="Your feedback" required>
|
||||
</label>
|
||||
<button type="submit">Submit Feedback</button>
|
||||
</form>
|
||||
<div id="feedback-list">
|
||||
|
||||
</div>
|
||||
</section>
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,184 +1,184 @@
|
||||
// Object called flowers that contains 3 flower objects with their information
|
||||
const flowers = {
|
||||
"rose": {
|
||||
name: "Rose",
|
||||
price: 2.5,
|
||||
category: "Romance",
|
||||
stock: 20,
|
||||
description: "The classic red rose for that someone special."
|
||||
},
|
||||
"tulip": {
|
||||
name: "Tulip",
|
||||
price: 3.0,
|
||||
category: "Springtime",
|
||||
stock: 15,
|
||||
description: "A perfect spring time flower for a bouquet."
|
||||
},
|
||||
"sunflower": {
|
||||
name: "Sunflower",
|
||||
price: 5.0,
|
||||
category: "Exclusives",
|
||||
stock: 10,
|
||||
description: "Large iconic flower perfect for an Autumn garden."
|
||||
}
|
||||
};
|
||||
|
||||
let feedback = [
|
||||
{flower: "Rose", rating: 5, content: "A beautiful bouqet delivered on time with care!"},
|
||||
{flower: "Tulip", rating: 4, content: "Reminds me of Spring any time of year!"},
|
||||
{flower: "Sunflower", rating: 5, content: "Sunflower was tall and healthy, so elegant!"}
|
||||
];
|
||||
|
||||
// Function to add flower information (price, stock, description, category) to HTML catalog
|
||||
function populateCatalog() {
|
||||
Object.values(flowers).forEach(flower => {
|
||||
const article = document.getElementById(`${flower.name.toLowerCase()}`)
|
||||
if (article) {
|
||||
const listItems = article.querySelectorAll("ul li");
|
||||
listItems[0].textContent = `Price: $${flower.price.toFixed(2)}`;
|
||||
listItems[1].textContent = `Category: ${flower.category}`;
|
||||
listItems[2].textContent = `Stock: ${flower.stock}`;
|
||||
listItems[3].textContent = `Description: ${flower.description}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Function to update items inside cart, used by addToCart() and at initialization
|
||||
function updateCartDisplay() {
|
||||
const tbody = document.querySelector("#cart-table tbody");
|
||||
tbody.innerHTML = "";
|
||||
let subtotal = 0;
|
||||
cart.forEach((item, idx) => {
|
||||
const row = document.createElement("tr");
|
||||
row.innerHTML = `
|
||||
<td>${flowers[item.name].name}</td>
|
||||
<td>$${item.price.toFixed(2)}</td>
|
||||
<td>${item.quantity}</td>
|
||||
<td>${(item.price * item.quantity).toFixed(2)}</td>
|
||||
<td><button data-idx="${idx}" class="remove-button">Remove</button></td>
|
||||
`
|
||||
tbody.appendChild(row);
|
||||
subtotal += item.price * item.quantity;
|
||||
});
|
||||
|
||||
const tax = subtotal * 0.13;
|
||||
const total = subtotal + tax;
|
||||
|
||||
document.getElementById("subtotal").textContent = subtotal.toFixed(2);
|
||||
document.getElementById("tax").textContent = tax.toFixed(2);
|
||||
document.getElementById("total").textContent = total.toFixed(2);
|
||||
|
||||
document.querySelectorAll(".remove-button").forEach(btn => {
|
||||
btn.addEventListener("click", function() {
|
||||
const idx = Number(btn.getAttribute("data-idx"));
|
||||
const item = cart[idx];
|
||||
flowers[item.name].stock += item.quantity;
|
||||
cart.splice(idx,1);
|
||||
updateCartDisplay();
|
||||
populateCatalog();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Function to handle getting the correct flower name and quantity to add to cart
|
||||
function setupCartForms() {
|
||||
document.querySelectorAll(".add-to-cart").forEach(form => {
|
||||
form.addEventListener("submit", function(e) {
|
||||
e.preventDefault();
|
||||
const article = form.closest("article");
|
||||
const flowerName = article.querySelector("h3").textContent;
|
||||
const quantity = form.querySelector("input[name='quantity']").value;
|
||||
addToCart(flowerName, quantity);
|
||||
form.reset();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Start with empty cart
|
||||
let cart = [];
|
||||
|
||||
// Funcion to add items to cart
|
||||
function addToCart(flowerName, quantity) {
|
||||
quantity = Number(quantity);
|
||||
|
||||
const key = flowerName.toLowerCase();
|
||||
const flower = flowers[key];
|
||||
|
||||
if (!flower || quantity < 1 || quantity > flower.stock) {
|
||||
alert("Too many or too few flowers added to cart, rejecting.");
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = cart.find(item => item.name === flowerName);
|
||||
if (existing) {
|
||||
if (quantity > flower.stock) {
|
||||
alert("Added quantity goes over stock limit, rejecting.");
|
||||
return
|
||||
}
|
||||
existing.quantity += quantity;
|
||||
} else {
|
||||
cart.push({
|
||||
name: key,
|
||||
price: flower.price,
|
||||
quantity: quantity
|
||||
});
|
||||
}
|
||||
flower.stock -= quantity;
|
||||
updateCartDisplay();
|
||||
populateCatalog();
|
||||
}
|
||||
|
||||
// Function to create feedback HTML elements from list
|
||||
function renderFeedback() {
|
||||
const feedbackList = document.getElementById("feedback-list");
|
||||
feedbackList.innerHTML = "";
|
||||
if (feedback.length === 0) {
|
||||
feedbackList.innerHTML = "<p>No feedback yet. Be our first review?</p>";
|
||||
return;
|
||||
}
|
||||
feedback.forEach(fb => {
|
||||
const div = document.createElement("div");
|
||||
div.className = "testimonial";
|
||||
div.innerHTML = `
|
||||
<strong>${fb.flower}</strong>
|
||||
<span>(${fb.rating}★)</span>
|
||||
<p>${fb.content}</p>
|
||||
<hr>
|
||||
`
|
||||
feedbackList.appendChild(div);
|
||||
})
|
||||
}
|
||||
|
||||
// Event listener for checkout button to show checkout message and reset cart
|
||||
document.getElementById("checkout-button").addEventListener("click", function() {
|
||||
if (cart.length === 0) {
|
||||
document.getElementById("checkout-message").textContent = "Your cart is empty!";
|
||||
return;
|
||||
}
|
||||
const confirmed = confirm("Are you sure you want to finalize cart and check out?");
|
||||
if (confirmed) {
|
||||
document.getElementById("checkout-message").textContent = "Thank you for your order!";
|
||||
cart = [];
|
||||
updateCartDisplay();
|
||||
populateCatalog();
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById("feedback-form").addEventListener("submit", function(e) {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const flower = form.flower.value;
|
||||
const rating = Number(form.rating.value);
|
||||
const content = form.content.value.trim()
|
||||
|
||||
if (!flower || !rating || !content) return;
|
||||
|
||||
feedback.unshift({ flower, rating, content });
|
||||
renderFeedback();
|
||||
form.reset();
|
||||
})
|
||||
|
||||
populateCatalog();
|
||||
setupCartForms();
|
||||
updateCartDisplay();
|
||||
// Object called flowers that contains 3 flower objects with their information
|
||||
const flowers = {
|
||||
"rose": {
|
||||
name: "Rose",
|
||||
price: 2.5,
|
||||
category: "Romance",
|
||||
stock: 20,
|
||||
description: "The classic red rose for that someone special."
|
||||
},
|
||||
"tulip": {
|
||||
name: "Tulip",
|
||||
price: 3.0,
|
||||
category: "Springtime",
|
||||
stock: 15,
|
||||
description: "A perfect spring time flower for a bouquet."
|
||||
},
|
||||
"sunflower": {
|
||||
name: "Sunflower",
|
||||
price: 5.0,
|
||||
category: "Exclusives",
|
||||
stock: 10,
|
||||
description: "Large iconic flower perfect for an Autumn garden."
|
||||
}
|
||||
};
|
||||
|
||||
let feedback = [
|
||||
{flower: "Rose", rating: 5, content: "A beautiful bouqet delivered on time with care!"},
|
||||
{flower: "Tulip", rating: 4, content: "Reminds me of Spring any time of year!"},
|
||||
{flower: "Sunflower", rating: 5, content: "Sunflower was tall and healthy, so elegant!"}
|
||||
];
|
||||
|
||||
// Function to add flower information (price, stock, description, category) to HTML catalog
|
||||
function populateCatalog() {
|
||||
Object.values(flowers).forEach(flower => {
|
||||
const article = document.getElementById(`${flower.name.toLowerCase()}`)
|
||||
if (article) {
|
||||
const listItems = article.querySelectorAll("ul li");
|
||||
listItems[0].textContent = `Price: $${flower.price.toFixed(2)}`;
|
||||
listItems[1].textContent = `Category: ${flower.category}`;
|
||||
listItems[2].textContent = `Stock: ${flower.stock}`;
|
||||
listItems[3].textContent = `Description: ${flower.description}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Function to update items inside cart, used by addToCart() and at initialization
|
||||
function updateCartDisplay() {
|
||||
const tbody = document.querySelector("#cart-table tbody");
|
||||
tbody.innerHTML = "";
|
||||
let subtotal = 0;
|
||||
cart.forEach((item, idx) => {
|
||||
const row = document.createElement("tr");
|
||||
row.innerHTML = `
|
||||
<td>${flowers[item.name].name}</td>
|
||||
<td>$${item.price.toFixed(2)}</td>
|
||||
<td>${item.quantity}</td>
|
||||
<td>${(item.price * item.quantity).toFixed(2)}</td>
|
||||
<td><button data-idx="${idx}" class="remove-button">Remove</button></td>
|
||||
`
|
||||
tbody.appendChild(row);
|
||||
subtotal += item.price * item.quantity;
|
||||
});
|
||||
|
||||
const tax = subtotal * 0.13;
|
||||
const total = subtotal + tax;
|
||||
|
||||
document.getElementById("subtotal").textContent = subtotal.toFixed(2);
|
||||
document.getElementById("tax").textContent = tax.toFixed(2);
|
||||
document.getElementById("total").textContent = total.toFixed(2);
|
||||
|
||||
document.querySelectorAll(".remove-button").forEach(btn => {
|
||||
btn.addEventListener("click", function() {
|
||||
const idx = Number(btn.getAttribute("data-idx"));
|
||||
const item = cart[idx];
|
||||
flowers[item.name].stock += item.quantity;
|
||||
cart.splice(idx,1);
|
||||
updateCartDisplay();
|
||||
populateCatalog();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Function to handle getting the correct flower name and quantity to add to cart
|
||||
function setupCartForms() {
|
||||
document.querySelectorAll(".add-to-cart").forEach(form => {
|
||||
form.addEventListener("submit", function(e) {
|
||||
e.preventDefault();
|
||||
const article = form.closest("article");
|
||||
const flowerName = article.querySelector("h3").textContent;
|
||||
const quantity = form.querySelector("input[name='quantity']").value;
|
||||
addToCart(flowerName, quantity);
|
||||
form.reset();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Start with empty cart
|
||||
let cart = [];
|
||||
|
||||
// Funcion to add items to cart
|
||||
function addToCart(flowerName, quantity) {
|
||||
quantity = Number(quantity);
|
||||
|
||||
const key = flowerName.toLowerCase();
|
||||
const flower = flowers[key];
|
||||
|
||||
if (!flower || quantity < 1 || quantity > flower.stock) {
|
||||
alert("Too many or too few flowers added to cart, rejecting.");
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = cart.find(item => item.name === flowerName);
|
||||
if (existing) {
|
||||
if (quantity > flower.stock) {
|
||||
alert("Added quantity goes over stock limit, rejecting.");
|
||||
return
|
||||
}
|
||||
existing.quantity += quantity;
|
||||
} else {
|
||||
cart.push({
|
||||
name: key,
|
||||
price: flower.price,
|
||||
quantity: quantity
|
||||
});
|
||||
}
|
||||
flower.stock -= quantity;
|
||||
updateCartDisplay();
|
||||
populateCatalog();
|
||||
}
|
||||
|
||||
// Function to create feedback HTML elements from list
|
||||
function renderFeedback() {
|
||||
const feedbackList = document.getElementById("feedback-list");
|
||||
feedbackList.innerHTML = "";
|
||||
if (feedback.length === 0) {
|
||||
feedbackList.innerHTML = "<p>No feedback yet. Be our first review?</p>";
|
||||
return;
|
||||
}
|
||||
feedback.forEach(fb => {
|
||||
const div = document.createElement("div");
|
||||
div.className = "testimonial";
|
||||
div.innerHTML = `
|
||||
<strong>${fb.flower}</strong>
|
||||
<span>(${fb.rating}★)</span>
|
||||
<p>${fb.content}</p>
|
||||
<hr>
|
||||
`
|
||||
feedbackList.appendChild(div);
|
||||
})
|
||||
}
|
||||
|
||||
// Event listener for checkout button to show checkout message and reset cart
|
||||
document.getElementById("checkout-button").addEventListener("click", function() {
|
||||
if (cart.length === 0) {
|
||||
document.getElementById("checkout-message").textContent = "Your cart is empty!";
|
||||
return;
|
||||
}
|
||||
const confirmed = confirm("Are you sure you want to finalize cart and check out?");
|
||||
if (confirmed) {
|
||||
document.getElementById("checkout-message").textContent = "Thank you for your order!";
|
||||
cart = [];
|
||||
updateCartDisplay();
|
||||
populateCatalog();
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById("feedback-form").addEventListener("submit", function(e) {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const flower = form.flower.value;
|
||||
const rating = Number(form.rating.value);
|
||||
const content = form.content.value.trim()
|
||||
|
||||
if (!flower || !rating || !content) return;
|
||||
|
||||
feedback.unshift({ flower, rating, content });
|
||||
renderFeedback();
|
||||
form.reset();
|
||||
})
|
||||
|
||||
populateCatalog();
|
||||
setupCartForms();
|
||||
updateCartDisplay();
|
||||
renderFeedback();
|
||||
@@ -1,82 +1,82 @@
|
||||
body {
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
background: #fafafa;
|
||||
color: #222;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
margin: 24px 0 12px 0;
|
||||
}
|
||||
|
||||
small {
|
||||
display: block;
|
||||
text-align: center;
|
||||
margin: 0 0 18px 0;
|
||||
color: #555;
|
||||
font-size: 0.98em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin-top: 32px;
|
||||
margin-bottom: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
section {
|
||||
margin: 0 auto 24px auto;
|
||||
max-width: 700px;
|
||||
}
|
||||
|
||||
article {
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
padding: 12px;
|
||||
margin: 12px 0;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
th, td {
|
||||
border: 1px solid #ccc;
|
||||
padding: 6px 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
form label {
|
||||
display: block;
|
||||
margin: 6px 0;
|
||||
}
|
||||
|
||||
input[type="number"], input[type="text"], select {
|
||||
padding: 2px 4px;
|
||||
margin-left: 4px;
|
||||
}
|
||||
|
||||
button {
|
||||
margin-top: 6px;
|
||||
padding: 4px 10px;
|
||||
border: 1px solid #bbb;
|
||||
border-radius: 3px;
|
||||
background: #f0f0f0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #e0e0e0;
|
||||
}
|
||||
|
||||
#feedback-list .testimonial {
|
||||
background: #f9f9f9;
|
||||
border: 1px solid #eee;
|
||||
border-radius: 4px;
|
||||
margin: 8px 0;
|
||||
padding: 8px;
|
||||
body {
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
background: #fafafa;
|
||||
color: #222;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
margin: 24px 0 12px 0;
|
||||
}
|
||||
|
||||
small {
|
||||
display: block;
|
||||
text-align: center;
|
||||
margin: 0 0 18px 0;
|
||||
color: #555;
|
||||
font-size: 0.98em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin-top: 32px;
|
||||
margin-bottom: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
section {
|
||||
margin: 0 auto 24px auto;
|
||||
max-width: 700px;
|
||||
}
|
||||
|
||||
article {
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
padding: 12px;
|
||||
margin: 12px 0;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
th, td {
|
||||
border: 1px solid #ccc;
|
||||
padding: 6px 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
form label {
|
||||
display: block;
|
||||
margin: 6px 0;
|
||||
}
|
||||
|
||||
input[type="number"], input[type="text"], select {
|
||||
padding: 2px 4px;
|
||||
margin-left: 4px;
|
||||
}
|
||||
|
||||
button {
|
||||
margin-top: 6px;
|
||||
padding: 4px 10px;
|
||||
border: 1px solid #bbb;
|
||||
border-radius: 3px;
|
||||
background: #f0f0f0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #e0e0e0;
|
||||
}
|
||||
|
||||
#feedback-list .testimonial {
|
||||
background: #f9f9f9;
|
||||
border: 1px solid #eee;
|
||||
border-radius: 4px;
|
||||
margin: 8px 0;
|
||||
padding: 8px;
|
||||
}
|
||||
Reference in New Issue
Block a user