Created
January 16, 2018 21:23
-
-
Save flangofas/1a1b55bd035f34bacd14757f9357ed56 to your computer and use it in GitHub Desktop.
JavaScript/DOM university assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add courses to the list (disallow adding same item twice) | |
// Remove courses from the list | |
// Limit courses by category | |
// Each course takes 5 credits, the list MUST have 6 courses | |
// Find desire course's category and suggest it on submit | |
// Course(or module) belongs to a Program | |
// Algebra belongs to math | |
// Linear belongs to math | |
// Up to 3 General | |
// Up to 4 Software | |
// Up to 4 Hardware | |
// Up to 2 Free | |
maximumCourses = 6; | |
let categories = { | |
general : { | |
title: 'General', | |
courses: [ | |
'G1', | |
'G2', | |
'G3', | |
'G4', | |
'G5', | |
'G6', | |
], | |
limit: 3, | |
selected: [] | |
}, | |
software : { | |
title: 'Software', | |
program: 'Computer Science', | |
courses: [ | |
'S1', | |
'S2', | |
'S3', | |
'S4', | |
'S5', | |
'S6', | |
], | |
limit: 4, | |
selected: [] | |
}, | |
hardware : { | |
title: 'Hardware', | |
program: 'Computer Engineering', | |
courses: [ | |
'H1', | |
'9H2', | |
'H3', | |
'H4', | |
'H5', | |
'H6', | |
], | |
limit: 4, | |
selected: [] | |
}, | |
free : { | |
title: 'Free electives', | |
courses: [ | |
'F1', | |
'F2', | |
'F3', | |
'F4', | |
'F5', | |
'F6', | |
], | |
limit: 2, | |
selected: [] | |
}, | |
}; | |
function add(course) { | |
let category = courseCategory(course); | |
if (!category) { | |
alert('Unknown category ' + category); | |
return; | |
} | |
//Do not add again the same course | |
if (categories[category].selected.indexOf(course) != -1) { | |
alert('Selected course ' + course + ' is alread selected'); | |
return; | |
} | |
//Do not add based on category's limit | |
if (categories[category].selected.length == categories[category].limit) { | |
alert('You are allowed to add only ' + categories[category].limit + ' of ' + category); | |
return; | |
} | |
//Prevent exceeding the maximum number of courses | |
if (totalSelected() == maximumCourses) { | |
alert('You have reached the maximum limit of courses.'); | |
return; | |
} | |
categories[category].selected.push(course); | |
printSelected(course); | |
printCreditsCounter(); | |
} | |
function remove(course) { | |
let category = courseCategory(course); | |
let index = categories[category].selected.indexOf(course); | |
if (index == -1) { | |
alert('Delete error, course ' + course + ' not found'); | |
return; | |
} | |
categories[category].selected.splice(index, 1); | |
//Print again the selected list | |
printSelected(); | |
printCreditsCounter(); | |
} | |
function printCreditsCounter() { | |
document.getElementById('counter').innerHTML = totalSelected() * 5; | |
} | |
function totalSelected() { | |
let total = 0; | |
for (category in categories) { | |
total += categories[category].selected.length; | |
} | |
return total; | |
} | |
function printSelected(course) { | |
let selected = document.getElementById('selected'); | |
selected.innerHTML = ""; | |
for (category in categories) { | |
for (index in categories[category].selected) { | |
let element = document.createElement('li'); | |
let text = document.createTextNode(categories[category].selected[index]); | |
element.appendChild(text); | |
selected.appendChild(element); | |
} | |
} | |
registerRemoveEvent(); | |
} | |
function courseCategory(course) { | |
for (category in categories) { | |
if (categories[category].courses.indexOf(course) !== -1) | |
return category; | |
} | |
return; | |
} | |
function loadLists() { | |
for (category in categories) { | |
let list = document.getElementById(category); | |
//Clear list | |
list.innerHTML = ""; | |
for (course in categories[category].courses) { | |
let element = document.createElement('li'); | |
let text = document.createTextNode(categories[category].courses[course]); | |
element.appendChild(text); | |
list.appendChild(element); | |
} | |
} | |
registerAddEvent(); | |
} | |
function submitCourses() { | |
let title = categories.software.title; | |
let program = categories.software.program; | |
if (categories.software.selected.length < categories.hardware.selected.length) { | |
title = categories.hardware.title; | |
program = categories.hardware.program; | |
} | |
// The rest of this code assumes you are not using a library. | |
// It can be made less wordy if you use one. | |
let form = document.createElement("form"); | |
form.setAttribute("method", 'POST'); | |
form.setAttribute("action", '/step3.php'); | |
let programField = document.createElement("input"); | |
programField.setAttribute("type", "hidden"); | |
programField.setAttribute("name", 'program'); | |
programField.setAttribute("value", program); | |
form.appendChild(programField); | |
let titleField = document.createElement("input"); | |
titleField.setAttribute("type", "hidden"); | |
titleField.setAttribute("name", 'title'); | |
titleField.setAttribute("value", title); | |
form.appendChild(titleField); | |
document.body.appendChild(form); | |
form.submit(); | |
} | |
function registerAddEvent() { | |
let all = document.getElementById('all-courses').getElementsByTagName("li"); | |
for (let i = 0; i < all.length; i++) { | |
all[i].addEventListener("click", (e) => { | |
add(e.target.textContent); | |
}); | |
} | |
} | |
function registerRemoveEvent() { | |
let selected = document.getElementById('selected').getElementsByTagName("li"); | |
for (let i = 0; i < selected.length; i++) { | |
selected[i].addEventListener("click", (e) => { | |
remove(e.target.textContent); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i dont understand