%%html
<html>
<head>
</head>
<body>
<button id="createUserButton">Create a New User</button>
<button onclick="displayPersonsList()">Display All Users</button>
<button onclick="clearUserData()">Clear User Data</button>
<ul id="personsList"></ul>
<script>
// Function to prompt the user for input and create a new person object
function createPersonObject() {
var name = prompt("Enter a name:");
var id_num = prompt("Enter an ID number:");
var age = prompt("Enter an age:");
var newPerson = {
name: name || null,
age: age ? parseInt(age) : null,
id_num: id_num || null,
classes: {},
interests: [],
favorite_songs: [],
num_games: null,
num_wins: null,
};
return newPerson;
}
// Check if local storage contains the array, if not, create an empty array
var personsArray = JSON.parse(localStorage.getItem('persons')) || [];
// Function to add a new person to the array and update local storage
function addPersonToArray(person) {
personsArray.push(person);
localStorage.setItem('persons', JSON.stringify(personsArray));
}
// Function to display all current objects in a list
function displayPersonsList() {
var list = document.getElementById('personsList');
list.innerHTML = ''; // Clear the existing list
for (var i = 0; i < personsArray.length; i++) {
var listItem = document.createElement('li');
listItem.textContent = JSON.stringify(personsArray[i]);
list.appendChild(listItem);
}
}
// Function to clear user data
function clearUserData() {
localStorage.removeItem('persons');
personsArray = [];
displayPersonsList(); // Clear the displayed list
}
// Add a click event listener to the "Create a New User" button
document.getElementById('createUserButton').addEventListener('click', function () {
var newUserPerson = createPersonObject();
addPersonToArray(newUserPerson);
displayPersonsList();
});
var person_one = {
name: "Yeongsu Kim",
age: 15,
classes: {1: "Honors Humanities", 2: "AP Chemistry", 3: "AP Calculus AB", 4: "AP World History", 5: "AP Computer Science Principles"},
interests: ["Music", "Video Games", "Candy", "Basketball"],
favorite_songs: ["Brahms Violin Concerto", "Tchaikovsky Violin Concerto", "Tchaikovsky Symphony no.5"],
id_num: "1951443",
num_games: 5,
num_wins: 2,
};
console.log(person_one);
person_one["birthday"] = "July 23, 2008";
person_one.interests.push("Computer Programming");
person_one.favorite_songs.push("Organ Symphony");
console.log(person_one);
if (person_one.num_games == 0 || person_one.num_wins == 0) {
console.log("Win Percentage: 0%");
} else {
console.log("Win Percentage: " + ((person_one.num_wins / person_one.num_games) * 100).toFixed(2) + "%");
}
console.log(typeof person_one);
console.log(typeof person_one.name);
console.log(typeof person_one.age);
console.log(typeof person_one.interests);
</script>
</body>
</html>