diff --git a/week 5/The BookList/index.html b/week 5/The BookList/index.html new file mode 100644 index 0000000..6ffa171 --- /dev/null +++ b/week 5/The BookList/index.html @@ -0,0 +1,46 @@ + + + + + + The BookList + + + + +
+

The BookList

+ +
+ +

+ +

+ +

+ +

+ +
+ + + + + + + + + + + + +
TitleGenreAuthorDateReadLast Read
+
+

Books marked as read: 0

+

Books marked not read yet: 0

+
+
+ + + + \ No newline at end of file diff --git a/week 5/The BookList/main.js b/week 5/The BookList/main.js new file mode 100644 index 0000000..9b1f42c --- /dev/null +++ b/week 5/The BookList/main.js @@ -0,0 +1,147 @@ + +// Book Class: Represents a book +class Book{ + constructor(title,genre,author,date){ + this.title = title; + this.genre = genre; + this.author = author; + this.date = date; + + } +} + +// BookList Class: Handles BookList Tasks +class BookList{ + static displayBooks(){ + const books = Store.getBooks(); + + // loop through the books + books.forEach(function(book){ + BookList.addBookToList(book) + }) + + + } + static addBookToList(book){ + const list = document.getElementById('book-list'); + + // create a table row element + + const row = document.createElement('tr'); + row.innerHTML = ` + ${book.title} + ${book.genre} + ${book.author} + ${book.date} + + + `; + + // Append the row to the list + + list.appendChild(row) + + } + + + static readBook(el){ + // const date = Date.now(); + if(el.classList.contains('complete-btn')){ + // el.parentElement.parentElement.classList.toggle('completed') + // el.parentElement.parentElement.innerHTML = `${book.date}`; + } + } + + + + //show alert + static showAlert(message,className){ + const div =document.createElement('div'); + div.className = `alert alert-${className}` + div.appendChild(document.createTextNode(message)); + const container = document.querySelector('.container'); + const form = document.getElementById('book-form'); + container.insertBefore(div, form); + // Make alert vanish after 3 seconds + setTimeout(function(){ + document.querySelector('.alert').remove() + }, 3000); + + } + + static clearFields(){ + document.getElementById('title').value = ''; + document.getElementById('genre').value = ''; + document.getElementById('author').value = ''; + document.getElementById('date').value = ''; + } +} + +//Store class: Handles storage +class Store { + static getBooks(){ + let books; + // check if there's a current 'book' item + if(localStorage.getItem('books') === null){ + books = []; + }else{ + //parse as json to be used as reg JS array of objs + books = JSON.parse(localStorage.getItem('books')); + } + return books; + } + static addBook(book){ + // Get currently stored books + const books = Store.getBooks(); + + //Add new book to 'books' array + books.push(book); + + //Set or save local storage with books array (converted to a string) + localStorage.setItem('books',JSON.stringify(books)); + } + +} + +//Event: Display Books +document.addEventListener('DOMContentLoaded',BookList.displayBooks) + +//Event: Add a Book +document.getElementById('book-form').addEventListener('submit',function(e){ + //Prevent default action + + e.preventDefault(); + + //Get form values + const title = document.getElementById('title').value + const genre = document.getElementById('genre').value + const author = document.getElementById('author').value + const date = document.getElementById('date').value + + // Validate form + if(title === '' || genre === '' || author === '' ||date === ''){ + BookList.showAlert(`Please fill in all fields`, 'danger'); + }else{ + // Call book class + const book = new Book(title,genre,author,date); + //console.log(book); + + // Add book to BookList + BookList.addBookToList(book); + + //Add book to store + Store.addBook(book); + + // Clear fields + BookList.clearFields(); + } +}) + +//Event: To mark a Book as Read + document.getElementById('book-list').addEventListener('click',function(e){ + BookList.readBook(e.target) + BookList.showAlert(`This is the current book being read`, 'success'); +}) + + + diff --git a/week 5/The BookList/style.css b/week 5/The BookList/style.css new file mode 100644 index 0000000..28d9742 --- /dev/null +++ b/week 5/The BookList/style.css @@ -0,0 +1,94 @@ +.container{ + margin-top: 20px; +} +.alert { + padding: 20px; + width: 70%; + color: white; + margin: auto; + text-align: center; + } + .alert-danger{ + background-color: #f44336; + } + .alert-success{ + background-color: #28a745; +} +.title{ + color: blue; + text-align: center; +} +#book-form{ + text-align: center; + padding: 10px; +} +input[type=text],input[type=date]{ + width: 25%; + padding: 6px; +} +.btn{ + background-color: blue; + color: #fff; + width: 25%; + padding: 7px; + border: none; + font-size: 20px; +} +.score{ + display: grid; + grid-template-columns: auto auto; + font-size: 1.2rem; + color: #fff; + +} +.score p:first-child{ + background: #28a745; + padding: 10px; + +} +.score p:last-child{ + background: #f44336; + padding: 10px; + +} +#table-head, #complete-table-head{ + text-align: center; + margin:40px ; + font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif; + border-collapse: collapse; + width: 70%; +} +#table-head th, #table-head td, #complete-table-head th, #complete-table-head td{ + border: 1px solid #ddd; + padding: 8px; +} +#table-head tr:nth-child(even){ + background-color: #f2f2f2; +} +#table-head tr:hover,#complete-table-head tr:hover{ + color: #ddd; +} +#table-head th{ + padding-top: 12px; + padding-bottom: 12px; + background-color: rgb(52, 52, 221); + color: #fff; +} +#complete-table-head{ + padding-top: 12px; + padding-bottom: 12px; + background-color: green; + color: #fff; +} +label{ + font-size: 20px; +} + +.complete-btn{ + background-color: lightseagreen; + margin: 5px; +} +.completed{ + text-decoration: line-through; + opacity: 0.7; +} \ No newline at end of file diff --git a/week 5/The Cat/index.html b/week 5/The Cat/index.html new file mode 100644 index 0000000..494ccf7 --- /dev/null +++ b/week 5/The Cat/index.html @@ -0,0 +1,16 @@ + + + + + + The Cat + + +

+

+

+

+ + + + \ No newline at end of file diff --git a/week 5/The Cat/main.js b/week 5/The Cat/main.js new file mode 100644 index 0000000..c017044 --- /dev/null +++ b/week 5/The Cat/main.js @@ -0,0 +1,33 @@ + + +let cat = { + name: "Jack", + tiredness: "tired", + hunger: "hungry", + lonliness: "lonely", + happiness: "happy", + + sleep(){ + //console.log(`${this.name} is very ${this.tiredness}`) + return `${this.name} is very ${this.tiredness}` + }, + + feed(){ + //console.log(`${this.name} is really ${this.hunger}`) + return `${this.name} is really ${this.hunger}` + }, + + feel(){ + //console.log(`${this.name} feel so ${this.lonliness}`) + return `${this.name} feel so ${this.lonliness}` + }, + + mood(){ + //console.log(`${this.name} is very ${this.happiness}`) + return `${this.name} is very ${this.happiness}` + } +} +document.getElementById('sleep').innerHTML = cat.sleep(); +document.getElementById('feed').innerHTML = cat.feed(); +document.getElementById('feel').innerHTML = cat.feel(); +document.getElementById('mood').innerHTML = cat.mood(); diff --git a/week 5/The Game(Roshambo)/index.html b/week 5/The Game(Roshambo)/index.html new file mode 100644 index 0000000..76b5da2 --- /dev/null +++ b/week 5/The Game(Roshambo)/index.html @@ -0,0 +1,37 @@ + + + + + + + + + + Rock Paper Scissors + + +
+
+

Rock Paper Scissors

+ +
+

Player: 0

+

Computer: 0

+
+
+

Make a Selection

+
+ + + +
+
+ + + + + + \ No newline at end of file diff --git a/week 5/The Game(Roshambo)/main.js b/week 5/The Game(Roshambo)/main.js new file mode 100644 index 0000000..fb6f512 --- /dev/null +++ b/week 5/The Game(Roshambo)/main.js @@ -0,0 +1,124 @@ +const choices = document.querySelectorAll('.choice'); +const score = document.getElementById('score'); +const result = document.getElementById('result'); +const restart = document.getElementById('restart'); +const modal = document.querySelector('.modal'); + +const scoreboard = { + player:0, + computer:0 +} + +// Play game + +function play(e){ + //console.log(e.target.id); + restart.style.display = 'inline-block' + const playerChoice = e.target.id; + const computerChoice = getComputerChoice() + const winner = getWinner(playerChoice,computerChoice); + showWinner(winner,computerChoice) + console.log(playerChoice,computerChoice,winner); +} + +// Get computer choice +function getComputerChoice(){ + const rand = Math.random(); + if(rand < 0.34){ + return 'rock' + } else + if(rand <= 0.67){ + return 'paper' + } else{ + return 'scissors' + } +} + +// Get Game winner +function getWinner(p,c){ + if(p === c){ + return 'draw'; + }else + if(p === 'rock'){ + if(c === 'paper'){ + return 'computer' + }else{ + return 'player'; + } + }else if(p === 'paper'){ + if(c === 'scissors'){ + return 'computer'; + }else{ + return 'player' + } + }else if (p === 'scissors'){ + if(c === 'rock'){ + return 'computer' + }else{ + return 'player' + } + } +} + +// show winner +function showWinner(winner,computerChoice){ + if(winner === 'player'){ + // increment player score + scoreboard.player++; + // show modal result + result.innerHTML = ` +

You Win

+ +

Computer Chose ${computerChoice}

+ `; + }else + if(winner === 'computer'){ + // increment computer score + scoreboard.computer++; + // show modal result + result.innerHTML = ` +

You Lose

+ +

Computer Chose ${computerChoice}

+ `; + }else{ + result.innerHTML = ` +

It's a Draw

+ +

Computer Chose ${computerChoice}

+ `; + } + // show score + score.innerHTML = ` +

Player: ${scoreboard.player}

+

Computer: ${scoreboard.computer}

+ `; + + //show modal + modal.style.display = 'block'; +} + +// Restart Game +function restartGame(){ + scoreboard.player = 0; + scoreboard.computer = 0 + score.innerHTML = ` +

Player: 0

+

Computer: 0

+ `; +} + +// Clear modal +function clearModal(e){ + if(e.target == modal){ + modal.style.display ='none' + } +} +// Event Listeners + +choices.forEach(function(choice){ + choice.addEventListener('click',play) +}); +window.addEventListener('click',clearModal); +restart.addEventListener('click', restartGame); + diff --git a/week 5/The Game(Roshambo)/modal.css b/week 5/The Game(Roshambo)/modal.css new file mode 100644 index 0000000..8d1b9ae --- /dev/null +++ b/week 5/The Game(Roshambo)/modal.css @@ -0,0 +1,31 @@ +.modal{ + display: none; + position: fixed; + margin-top: 170px; + z-index: 1; + left: 0; + top: 0; + height: 100%; + width: 100%; + overflow: auto; + background-color: rgba(0, 0, 0, 0.5); + } + + .modal-content{ + background-color: #fff; + text-align: center; + margin: 10px auto; + width: 350px; + border-radius: 10px; + padding: 3rem; + box-shadow: 0 5px 8px o rgba(0, 0, 0, 0.2), 0 7px 10px 0 rgba(0, 0, 0, 0.17); + animation-name: modalopen; + animation-duration: 1s; + } + .modal-content h1{ + margin-bottom: 1rem; + } + .modal-content p{ + font-size: 1.2rem; + margin-top: 1rem; + } \ No newline at end of file diff --git a/week 5/The Game(Roshambo)/style.css b/week 5/The Game(Roshambo)/style.css new file mode 100644 index 0000000..f2bcce0 --- /dev/null +++ b/week 5/The Game(Roshambo)/style.css @@ -0,0 +1,85 @@ + +*{ + box-sizing: border-box; + padding: 0; + margin: 0; +} +body{ + font-family: 'Roboto', 'sans-serif'; + line-height: 1.6; + background: #fff; + color: #333; +} +.container{ + width: 1100px; + margin: auto; + overflow: hidden; + padding: 0.2rem; + text-align: center; +} +.restart-btn{ + display: none; + background-color: #f4f4f4; + color: #333; + padding: 10px; + font-size: 1rem; + outline: none; + border: none; + cursor: pointer; + margin-bottom: 1rem; +} +.restart-btn:hover{ + background-color: #003699; + color: #fff; +} +.header{ + text-align: center; + margin: 1rem 0; + +} +.header h1{ + margin-bottom: 1rem; +} +.score{ + display: grid; + grid-template-columns: auto auto; + font-size: 1.2rem; + color: #fff; +} +.score p:first-child{ + background: #003699; + +} +.score p:last-child{ + background: #333333; + +} +.choices{ + display: grid; + grid-template-columns: auto auto auto; + grid-gap: 2rem; + margin-top: 3rem; + text-align: center; +} +.choice{ + cursor: pointer; +} +.choice:hover{ + color: #003699; +} +.text-win{ + color: #28a745; +} +.text-lose{ + color: #dc3545; +} +@media(max-width: 700px){ + .choice{ + font-size: 110px; + } +} +@media(max-width: 500px){ + .choice{ + font-size: 80px; + } +} \ No newline at end of file diff --git a/week 6/app.js b/week 6/app.js new file mode 100644 index 0000000..a7c9cad --- /dev/null +++ b/week 6/app.js @@ -0,0 +1,40 @@ +/* Closure in Javascript +Closure is created when an inner function has access to its outer function variables and arguments. + In JavaScript, closures are created every time a function is created. + The inner function has access to – +1. Its own variables. +2. Outer function's variables and arguments. +3. Global variables. + +*/ + + +var a = 1; + +function someFunction(number) { + function otherFunction(input) { + return a; + } + + a = 5; + + return otherFunction; +} + +var firstResult = someFunction(9); +var result = firstResult(2); + /*The Result is "there's a type error" because firstResult + is not a function, making it undefined. */ + + +var a = 1; +function b() { + a = 10; + return; + function a() {} +} +b(); +console.log(a); +/* Answer on console is 1 because it is declared and initialized as 1 in the global scope even though + it is re-assigned in the function b() which does not return any value. +*/ diff --git a/week 7/promises.js b/week 7/promises.js new file mode 100644 index 0000000..cf0166e --- /dev/null +++ b/week 7/promises.js @@ -0,0 +1,36 @@ +/* +Syncronous & Asyncronous Javascript (MVP) + +1. A promise is an object that may produce a single value some time in the future: + either a resolved value, or a reason that it’s not resolved + A promise may be in one of 3 possible states: fulfilled, rejected, or pending. + Promise users can attach callbacks to handle the fulfilled value or the reason for rejection. + + 2. Synchronous operations block instructions until the task is completed, while + Asynchronous operations can execute without blocking other operations. + Asynchronous operations are generally completed by firing an event or by calling a + provided callback function. + + 3. Synchronous sample code + console.log('First'); + console.log('Second'); + console.log('Third'); + Output to the console will be + First + Second + Third + + Asynchronous sample code + let promise = new Promise(function(resolve, reject) { + setTimeout(() => resolve("done!"), 1000); + }); + promise.then( + result => alert(result), // shows "done!" after 1 second + error => alert(error) // doesn't run + ); + + 4. Different ways to deal with Asynchronous Code + 1) Callbacks + 2) Promises + 3) Async or Await +*/ \ No newline at end of file diff --git a/week3-4/MVP/index.html b/week3-4/MVP/index.html new file mode 100644 index 0000000..877a5fe --- /dev/null +++ b/week3-4/MVP/index.html @@ -0,0 +1,25 @@ + + + + + + ToDo-List + + + +
+

ToDo-List

+
+ + + +
+
+ +
+ +
+ + + + \ No newline at end of file diff --git a/week3-4/MVP/main.js b/week3-4/MVP/main.js new file mode 100644 index 0000000..ffe6cab --- /dev/null +++ b/week3-4/MVP/main.js @@ -0,0 +1,59 @@ +let txtval = document.getElementById("txtval"); +let addbtn = document.getElementById("addbtn"); +let divdisplay = document.getElementById("divdisplay"); + +let lists = []; +let indexval = 0; + + +function checkBtn(){ + if(addbtn.innerHTML=="Update"){ + Updateval(); + }else + if(addbtn.innerHTML=="Add"){ + addval(); + } +} + +function addval(){ + if (txtval.value==""){ + txtval.placeholder = `Add an item to the list`; + }else{ + var inputval= txtval.value; + lists.push(inputval); + txtval.value=""; + render(); + } + + +} + +function render(){ + var txt= ""; + divdisplay.innerHTML=txt; +} + +function deleteBtn(index){ + delete lists[index]; + render(); + +} + +function editBtn(index){ + txtval.value=lists[index]; + addbtn.innerHTML='Update'; + indexval=index; +} + +function Updateval(){ + lists[indexval]=txtval.value; + addbtn.innerHTML='Add'; + txtval.value=""; + render(); +} + diff --git a/week3-4/MVP/style.css b/week3-4/MVP/style.css new file mode 100644 index 0000000..fbddda2 --- /dev/null +++ b/week3-4/MVP/style.css @@ -0,0 +1,50 @@ +body{ + background-color: #fff; + margin: 0, auto; +} +h1{ + text-align: center; + font-size: 40px; + color: #ec2222; +} + +.inputBtn{ + text-align: center; +} +#divdisplay{ + text-align: center; +} +#txtval{ + padding: 5px; + width: 250px; + border-radius: 5px; + border: 2px solid #5632da; + margin-right: 7px; + +} +.btnDis{ + color: #fff; + background-color: red; + border: transparent; + padding: 10px; + margin: 10px; + cursor: pointer; + font-size: 16px; +} +.btnDis:hover{ + background-color: #fff; + color: black; + border: 2px solid red; +} +#divdisplay ul li{ + list-style: none; + cursor: pointer; +} +/*.complete-btn{ + background-color: lightseagreen; + margin: 5px; +} +.completed{ + text-decoration: line-through; + opacity: 0.7; +} */ \ No newline at end of file diff --git a/week3-4/calculator/index.html b/week3-4/calculator/index.html new file mode 100644 index 0000000..dc45920 --- /dev/null +++ b/week3-4/calculator/index.html @@ -0,0 +1,45 @@ + + + + + + Calculator + + + +

JavaScript Calculator

+
+
+ +
+ +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+
+ + + + \ No newline at end of file diff --git a/week3-4/calculator/main.js b/week3-4/calculator/main.js new file mode 100644 index 0000000..79cec3b --- /dev/null +++ b/week3-4/calculator/main.js @@ -0,0 +1,53 @@ +function numOne(){ + document.lcdform.inputVals.value += '1'; +} + +function numTwo(){ + document.lcdform.inputVals.value += '2' +} +function numThree(){ + document.lcdform.inputVals.value += '3' +} +function numFour(){ + document.lcdform.inputVals.value += '4' +} +function numFive(){ + document.lcdform.inputVals.value += '5' +} +function numSix(){ + document.lcdform.inputVals.value += '6' +} +function numSeven(){ + document.lcdform.inputVals.value += '7'; +} +function numEight(){ + document.lcdform.inputVals.value += '8' +} +function numNine(){ + document.lcdform.inputVals.value += '9' +} +function zero(){ + document.lcdform.inputVals.value += '0' +} +function dot(){ + document.lcdform.inputVals.value += '.' +} +function operationPlus(){ + document.lcdform.inputVals.value += '+' +} +function operationMinus(){ + document.lcdform.inputVals.value += '-' +} +function operationMult(){ + document.lcdform.inputVals.value += '*' +} +function operationDiv(){ + document.lcdform.inputVals.value += '/' +} +function equalTo(){ + document.lcdform.inputVals.value = eval(lcdform.inputVals.value) +} +function clr(){ + document.lcdform.inputVals.value = '' +} + diff --git a/week3-4/calculator/style.css b/week3-4/calculator/style.css new file mode 100644 index 0000000..7585c61 --- /dev/null +++ b/week3-4/calculator/style.css @@ -0,0 +1,41 @@ +body{ + background-color: #fff; + margin: 0px auto; +} +.calbody{ + background-color: #594ef1; + border: 1px solid #ff0; + padding: 6px; + margin-left: 450px; + width:340px; +} +h1{ + text-align: center; + font-size: 40px; + color: #ff3077; +} +#inputVal{ + color: black; + text-align: right; + width: 325px; + height: 35px; + font-size: 20px; +} +button{ + background-color: #fff; + width: 80px; + height: 50px; + font-size: 20px; + border: 1px solid #594ef1; +} +button:hover{ + background-color: #594ef1; +} +.clear{ + background-color: #ff3077; +} + +/*#equalTo{ + width: 175px; + +}*/ \ No newline at end of file