Personal homepage JS

Oluwatoyin
Javascript_DOM_Functions_Objects_Events_FinalFiles.zip

Javascript_DOM_Functions_Objects_Events/css/styles.css

body{ float: left; width: 100%; padding: 0; margin: 0; font-family: calibri, arial, sans-serif; } header{ position: fixed; top: 0; left: 0; width: 96%; height: 75px; padding: 20px 2%; border-bottom: 2px solid black; background: #f7f7f7; z-index: 5; color: #008B95; } header .left{ font-size: 36px; } header .right{ font-size: 18px; } .left{ float: left; width: 35%; } .right{ float: right; width: 60%; text-align: right; } .big{ font-weight: 900; text-transform: uppercase; } .small{ font-weight: 400; text-transform: lowercase; } #container{ float: left; width: 100%; height: 500px; margin-top: 150px; } article{ float: right; width: 98%; padding-top: 2%; min-width: 490px; height: auto; } h2{ float: left; clear: both; width: 50%; margin: 10px 0 5px 5%; } .clear{ float: left; width: 100%; clear: both; } footer{ float: left; width: 100%; padding: 20px 0; margin-top: 30px; border-top: 5px double black; } .ftrLeft{ padding-left: 2%; } .ftrRight{ padding-right: 2%; } .orange{ background: #E98E4E; } .orangeHover{ background: #f0b083; } .blueGreen{ background: #6CA3B2; } .lightAqua{ background: #99D1D5; } .lightAquaHover{ background: #b8dfe2; } /*Lot*/ .row{ width: 100%; height: 12%; float: left; } .col-3{ width: 24%; } .col-6{ width: 49%; } .col-9{ width: 74%; } .button{ height: 80%; line-height: 100%; margin: 1.25px .5%; float: left; background: #6CA3B2; color: white; text-align: center; font-size: 48px; } .button:hover{ cursor: pointer; background: #b8dfe2 !important; } #carLot{ width: 25%; height: 420px; margin-left: 2%; padding: 2%; float: left; background: rgba(0, 0, 0, .1); color: white; border: 2px solid black; } #console{ width: 64%; height: 470px; margin-right: 2%; float: right; font-size: 18px; border: 2px solid black; overflow: scroll; } #console div b{ text-decoration: underline; } #consoleLeft{ height: 100%; width: 30%; border-right: 1px solid rgba(0, 0, 0, .5); } #consoleRight{ height: 100%; /*width: 56%;*/ float: left; } #consoleLeft, #consoleRight{ padding: 2%; text-align: left; overflow: scroll; } .make{ float: left; clear: both; width: 100%; height: 145px; } .makeName{ float: left; clear: both; width: 100%; height: 30px; line-height: 30px; font-size: 24px; color: black; } .car{ float: left; height: 100px; width: 15%; margin: 2%; border: 1px solid black; color: black; background: #99D1D5; box-shadow: 3px 3px 3px 0 rgba(0, 0, 0, .3); } .car:hover, .car:active{ background: #b8dfe2; } .car:active{ font-weight: bold; border-width: 3px; } .roof{ float: left; width: 96%; height: 50%; margin-top: 40%; padding: 2%; line-height: 50px; text-align: center; border-top: 1px solid black; border-bottom: 1px solid black; } .roof:active{ border-top-width: 3px; border-bottom-width: 3px; }

Javascript_DOM_Functions_Objects_Events/images/favicon.ico

Javascript_DOM_Functions_Objects_Events/index.html

NEIU CS300 Javascript DOM, Functions & Events Honda ACRD ODSY CRV PLT Toyota CMRY SNA CRLA Nissan RGE ALTM MXMA SNTR ARMD Computer User No Cars Selected Northeastern Illinois University - CS300 © Copyright 2018

Javascript_DOM_Functions_Objects_Events/scripts/cars.js

/* * using a function to create an object */ function Car(make, model, year, doors, price){ this.make = make; this.model = model; this.year = year; this.doors = doors; this.price = price; /* * Privelaged function -> a function belonging to the object */ this.msrp = function(){ /* * toLocaleString -> allows us to format text according to region */ return price.toLocaleString('en-US', { style: 'currency', currency: 'USD' }); } this.monthlyPayments = function(term){ var amount = this.price / term; return amount.toLocaleString('en-US', { style: 'currency', currency: 'USD' }); } } var accord = new Car('Honda', 'Accord', 2017, 4, 26000); var odyssey = new Car('Honda', 'Odyssey', 2018, 4, 35000); var crv = new Car('Honda', 'CRV', 2017, 4, 17500); var pilot = new Car('Honda', 'Pilot', 2018, 4, 32595); var camry = new Car('Toyota', 'Camry', 2018, 4, 27500); var sienna = new Car('Toyota', 'Sienna', 2018, 4, 34500); var corolla = new Car('Toyota', 'Corolla', 2017, 4, 23250); var rogue = new Car('Nissan', 'Rogue', 2018, 4, 19570); var altima = new Car('Nissan', 'Altima', 2018, 4, 28200); var maxima = new Car('Nissan', 'Maxima', 2018, 2, 31000); var sentra = new Car('Nissan', 'Sentra', 2017, 4, 16500); var armada = new Car('Nissan', 'Armada', 2017, 4, 32250); var cars = [accord, odyssey, crv, pilot, camry, sienna, corolla, rogue, altima, maxima, sentra, armada];

Javascript_DOM_Functions_Objects_Events/scripts/scripts.js

/* * document.getElementById(id) -> single node * ToDo: Get #consoleLeft and #consoleRight */ var consoleLeft = document.getElementById('consoleLeft'); var consoleRight = document.getElementById('consoleRight'); /* * document.getElementsByClassName(class) -> nodeList * ToDo: Get all .car */ var allCars = document.getElementsByClassName('car'); /* * document.querySelectorAll(selector) -> nodeList * ToDo: Get all #toyotas .car and #nissans .car */ var toyotas = document.querySelectorAll('#toyotas .car'); var nissans = document.querySelectorAll('#nissans .car'); /* * document.getElementsByTagName(tagName) --> return a node list with every element of this type */ var paragraphs = document.getElementsByTagName('p'); /* * document.createElement(tagToCreate) -> new tag * ToDo: Create a new paragraph */ var newParagraph = document.createElement('p'); /* * [element].innerHTML -> set the HTML of a node (may include tags) * ToDo: Set HTML of new paragraph to list the count of cars in the dealer lot */ newParagraph.innerHTML = '<b>There are: ' + allCars.length + ' cars in the lot</b>'; /* * [element].style -> set a desired CSS property (use camelCase instead of hyphens) * ToDo: Change new paragraph text color to red */ newParagraph.style.color = 'red'; /* * [element].setAttribute(attrName, attrValue) -> set a desired attribute * ToDo: Set id for new paragraph to 'allCars' */ newParagraph.setAttribute('id', 'allCars'); /* * [element].appendChild(newElement) -> adds new element as the last child of the parent * ToDo: Add new paragraph to #consoleLeft */ consoleLeft.appendChild(newParagraph); //Create another new paragraph newParagraph = document.createElement('p'); /* * document.createTextNode('text') -> new text node (i.e. text only) * ToDo: Create a new text node and set text to be # of Toyotas on the dealer lot * - Add the new text to the paragraph * - Add the paragraph to #consoleLeft */ var txt = document.createTextNode('Number of Toyotas: ' + toyotas.length); newParagraph.appendChild(txt); consoleLeft.appendChild(newParagraph); //Create another new paragraph newParagraph = document.createElement('p'); //Set the text to be # of Nissans on the dealer lot newParagraph.textContent = 'Number of Nissans: ' + nissans.length; consoleLeft.appendChild(newParagraph); //Create a new ul var list = document.createElement('ul'); /* * forEach -> loop through a collection/list * ToDo: Loop thru all nissans * - Create an li for each nissan * - Get the model from name attribute * - Make text the model using [element].textContent * * Inside forEach loop * [element].getAttribute(attr) -> gets the value of an attribute */ nissans.forEach(function(nissan) { var listItem = document.createElement('li'); var name = nissan.getAttribute('name'); listItem.textContent = name; list.appendChild(listItem); }); //Add ul to #consoleLeft consoleLeft.appendChild(list); /* * [element].insertBefore(newElement, existingElement) -> adds new element before a specific child * ToDo: Add new paragraph to #consoleLeft before the ul */ newParagraph = document.createElement('p'); newParagraph.innerHTML = '<b>NISSANS</b>'; consoleLeft.insertBefore(newParagraph, list); /* * function -> Javascript version of a method * ToDo: Create printCarDetails(carDiv) * - Loop thru cars and compare model to name attribute (getAttribute) * - Get Car Details and output to #consoleRight */ function printCarDetails(carDiv){ var name = carDiv.getAttribute('name'); //Loop thru cars and find selected car object var selectedCar = cars.filter(x => x.model == name)[0]; //Get #carTitle var carTitle = document.getElementById('carTitle'); //Create h5 and give id carTitle var newCarTitle = document.createElement('h5'); newCarTitle.setAttribute('id', 'carTitle'); /* * [element].textContent -> another way to set a node's text (no tags allowed) * ToDo: Set h5 text to car make and model */ newCarTitle.textContent = selectedCar.make + ' ' + selectedCar.model; /* * [element].replaceChild(newElement, existingElement) -> replace existing child with new element * ToDo: Replace h5 with carTitle */ consoleRight.replaceChild(newCarTitle, carTitle); /* * ToDo: Create new paragraph * - Set innerHTML to be year, # of doors, MSRP, 72 monthly payments */ var newParagraph = document.createElement('p'); newParagraph.innerHTML = 'Year: ' + selectedCar.year + '<br>' + 'Doors: ' + selectedCar.doors + '<br>' + 'MSRP: ' + selectedCar.msrp() + '<br>' + '72 Monthly Payments: ' + selectedCar.monthlyPayments(72); //Find all existing #consoleRight p and remove 1st one /* * Inside if block * [element].removeChild(toRemove) -> remove existing child */ var existingParagraphs = document.querySelectorAll('#consoleRight p'); if(existingParagraphs.length > 0) consoleRight.removeChild(existingParagraphs[0]); //Add new paragraph to #consoleRight consoleRight.appendChild(newParagraph); } /* * addEventListener -> add a 'listener' so we can respond to a specific action * ToDo: Add 'click' event listener for all Toyotas to go to printCarDetails(this) * ToDo: Add 'click' event listener for all Nissans to go to printCarDetails(this) */ toyotas.forEach(function(toyota){ toyota.addEventListener('click', function(){ printCarDetails(this); }); }); nissans.forEach(function(nissan){ nissan.addEventListener('click', function(){ printCarDetails(this); }); }); /* * setInterval(codeToExecute, milliseconds) -> executes something at a specified interval (every 'x' amount of milliseconds) * ToDo: Set an interval that will update the count of Hondas on the lot once every second */ var hondaCount = document.getElementById('hondaCount'); hondaCount.textContent = 1; var hondaInterval = setInterval(function(){ hondaCount.textContent++; }, 1000); var toyotaCount = document.getElementById('toyotaCount'); toyotaCount.textContent = 1; var toyotaInterval = setInterval(function(){ toyotaCount.textContent++; }, 1000); /* * setTimeout(codeToExecute, milliseconds) -> executes the specified code ONCE after 'x' amount of time * clearInterval(intervalName) -> stops the interval * ToDo: Since there are only 4 Hondas on the lot, stop the counter after 4 seconds. Set a timeout for 4 seconds that will clear the interval */ setTimeout(function(){ clearInterval(hondaInterval); }, 3000); setTimeout(function(){ clearInterval(toyotaInterval); }, 2000); //Set an interval that will update the count of Toyotas on the lot once every second. Stop counting once you've reached the number of toyotas