diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..aef844305 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/01week/datatypes.html b/01week/datatypes.html new file mode 100644 index 000000000..8a73fe1a8 --- /dev/null +++ b/01week/datatypes.html @@ -0,0 +1,18 @@ + + + + + + + +

JS 211

+ +

+
'A' is declared as a
+
The sum of 5 + 11 is
+ +
The data type of 5 is a
+ + + + \ No newline at end of file diff --git a/01week/datatypes.js b/01week/datatypes.js index e69de29bb..fd0a3a24e 100644 --- a/01week/datatypes.js +++ b/01week/datatypes.js @@ -0,0 +1,177 @@ +'use strict' + +//program to display current day and time + +/*let now = new Date(); +console.log("the current date and time", now); + +//write js program to convert num to string +const theNumber = 5; +const theString = theNumber.toString(); + +console.log("The type of theString variable is", (typeof theString)) +console.log("The type of theNumber variable is",(typeof theNumber)) +console.log("The string is ", theNumber.toString()); +console.log("the number is", theNumber); + +//covert a string to the number +const theOtherString = "4.5"; +const theOtherNumber = parseInt(theOtherString, 10); +console.log("The otherString type is", (typeof theOtherString)); +console.log("The otherNumber") + + +(counting in base 10) +0,1,2,3,4,5,6,7,8,9,10,11 + +(counting in base 2) +0, 1, 10, 11, 100, 111, 1000 + +(counting in base 16) +0, 1, 2, 3, 4, 5, 6, 7, 8, 9,A , B, C, D ,E ,F +*/ +// **THIS IS INCREDIBLY IMPORTANT THAT YOU DO BOTH SECTIONS!!! You will be doing only front-end work in 421 and you need to brush up on your HTML elements** + + +// *************************** +// PART ONE +// *************************** +// Write a JavaScript program to display the current day and time, start with: +//console.log(new Date) + +const displayDate = () => { + const currentDate = new Date() + + document.getElementById("showDate").innerHTML = currentDate; +} + + + +// Write a JavaScript program to convert a number to a string. + +const num = 100; +console.log(num, typeof num); + +const grade = String(num); +console.log(grade, typeof grade) + + + +// Write a JavaScript program to convert a string to the number. +const amount = "10.25" +console.log(amount, typeof amount); + +const a = parseFloat(amount); +console.log(a, typeof a); + +// Write a JavaScript program that takes in different datatypes and prints out whether they are a: +// * Boolean +let untrue = false; +//console.log(a, typeof a) +document.getElementById("booleanTest").innerText = typeof a; + + +// * Null +let b = null; +console.log(b, typeof b) + +// * Undefined +let c; +console.log(c, typeof c) + +// * Number +document.getElementById("numDataType").addEventListener("click", function(){ +const number = 5; + document.getElementById("typeOfNum").innerHTML = typeof number; +}); + +// * NaN +const NotANum = NaN; +console.log(NotANum, typeof NotANum) + +// * String +const greeting = "Hello there"; +console.log(greeting, typeof greeting) + + + +// Write a JavaScript program that adds 2 numbers together. +const displaySum = () => { +const num1 = 5; +const num2 = 11; + +const total = num1 + num2; +/*document.getElementById("sum").addEventListener("click", function(){ + const num1 = 5; + const num2 = 11; + const total = num1 + num2; + document.getElementById("sumOfTwoNum").innerHTML = total; +});*/ + +document.getElementById("sum").innerHTML = total; +} + +// Write a JavaScript program that runs only when 2 things are true. +const value1 = 10; +const value2 = 20; + +const bothAretrue = (arg1, arg2) => { + if (arg1 && arg2){ + return true + } else { + return false + } +} + +truth(value1, value2) + + + +// Write a JavaScript program that runs when 1 of 2 things are true. + +const numeroUno = 13; +const numeroDos = 0; + +const oneIsTrue = (argue1, argue2) => { + if (argue1 || argue2){ + return true + } else { + return false + } +} + +oneIsTrue(numeroUno, numeroDos) +// Write a JavaScript program that runs when both things are not true. + +const empty = " "; +const zero = 0; + +const bothAreFalse = (argument1, argument2) => { + if (argument1 && argument2){ + return true + } else { + return false + } +} + +oneIsFalse(empty, zero) + +// *************************** +// PART TWO +// *************************** + +// 1. download Live-Server by Ritwick Dey, +// 2. reload VS Code, +// 3. click the "Go Live" button +// 4. Go local host 5500 or http://127.0.0.1:5500/index.html to see your web page +// 5. Or go use the `npm start` command and navigate to localhost:8080 (ctrl + C to close) +// 6. go to `index.html` +// 7. create inputs, buttons and event listeners that render the code blocks you built above to the DOM. + + + + +// Additional Resources +// Video1: https://player.vimeo.com/video/377147232 +// Video2: https://www.youtube.com/embed/bkvH28PXLWc +// Video3: https://www.youtube.com/embed/TrGI9Yki-24 diff --git a/01week/helloworld.js b/01week/helloworld.js new file mode 100644 index 000000000..3451e9b7e --- /dev/null +++ b/01week/helloworld.js @@ -0,0 +1 @@ +console.log('Hello World'); diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index 72f0f1a89..11d55022e 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -14,10 +14,41 @@ const rl = readline.createInterface({ // the function that will be called by the unit test below const rockPaperScissors = (hand1, hand2) => { +hand1 = hand1.toLowerCase().trim(); +hand2 = hand2.toLowerCase().trim(); + + if (hand1 === hand2) { + return "It's a tie!" + } else if (hand1 === 'rock'){ + if (hand2 === 'paper'){ + return "hand2 wins" + } else if (hand2 === 'scissors'){ + return "hand1 wins!" + } +} + +else if (hand1 === 'paper'){ + if (hand2 === 'scissors'){ + return "hand2 wins!" + } else if (hand2 === 'rock'){ + return "hand1 wins!" + } +} + + else if (hand1 === 'scissors'){ + if(hand2 === 'rock'){ + return "hand2 wins!" + } else if (hand2 === 'paper'){ + return "hand1 wins!" + } + } + } + + // Write code here // Use the unit test to see what is expected -} + // the first function called in the program to get an input from the user // to run the function use the command: node main.js diff --git a/02week/pigLatin.html b/02week/pigLatin.html new file mode 100644 index 000000000..c4d6b9f38 --- /dev/null +++ b/02week/pigLatin.html @@ -0,0 +1,23 @@ + + + + + Document + + + +

Pig Latin

+
+ + +
+
+ +
+

+ + + + + + \ No newline at end of file diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 7f2b58a8a..8f649d061 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -1,18 +1,82 @@ -'use strict'; +/*'use strict'; const assert = require('assert'); const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout -}); +});*/ + +var userInput = document.getElementById("Input"); +var userOutput = document.getElementById("Output"); + +/*document.getElementById("Translate").onlick = function(){ + userOutput.value = pigLatin(userInput.value); + console.log("I got clicked"); +}; +*/ + +document.getElementById("btn").onclick = function(){ + userOutput.value = pigLatin(userInput.value); +} + const pigLatin = (word) => { + word = word.toLowerCase().trim(); + if(checkWord(word.charAt(0))){ + return word + "yay"; + }else if(checkWord(word.charAt(1))){ + //else if(checkWord(word.indexOf[1])) + return word.slice(1) + word.charAt(0) + 'ay'; + } + else if(checkWord(word.charAt(2))){//?? two word slice methods below??? + return word.slice(2) + word.slice(0, 2) + 'ay'; + } + else if(checkWord(word.charAt(3))){ + return word.slice(3) + word.slice(0,3) + 'ay'; + } + else + return "Invalid input" +}; + +const checkWord = (word) =>{ + //go through the word and + //the if statement checks the word for the vowel + if(word === 'a' || word === 'e' || word === 'i' || word === 'o' || word === 'u'){ + return true; + //seperate the word where the vowel is found, and implement the part1, part2,part3 values + } + else{ + return false; + } + +}; +console.log(checkWord('dog')); +console.log(pigLatin('valley')); +console.log("Hello"); +//pigLatin( ,); +//?????? +//vowelCheck = word.match(/aeiou/gi); +//use a for loop to find tne position of the first vowel +//use indexOf to find the vowel + + + + + +//1. if the word starts with a vowel, add "yay" to the end +//2. if the word has a vowel: +// a. find the first vowel. +// b. split the word into 2 parts: +// - starting at the beginning and stopping just before the first vowel +// - starting at the first vowel and to the end of the word +// c. make a new word, where you flip the 2 parts +// d. add "ay" to the end of the new word` // Your code here -} + const getPrompt = () => { @@ -48,4 +112,4 @@ if (typeof describe === 'function') { getPrompt(); -} +}; diff --git a/02week/pigLatinDOM.js b/02week/pigLatinDOM.js new file mode 100644 index 000000000..36600f040 --- /dev/null +++ b/02week/pigLatinDOM.js @@ -0,0 +1,10 @@ +for(let i >= 1; i <= 100; i++){ + if(i % 3 ===0 && i % 5=== 0){ + console.log("FizzBuzz"); + } + else if(i % 3 === 0){ + console.log("Fizz"); + }else if(i % 5 ===0){ + console.log("Buzz"); + } +}; diff --git a/02week/ticTacToe.hmtl b/02week/ticTacToe.hmtl new file mode 100644 index 000000000..e69de29bb diff --git a/02week/todo.html b/02week/todo.html new file mode 100644 index 000000000..6ad57a395 --- /dev/null +++ b/02week/todo.html @@ -0,0 +1,19 @@ + + + + + Document + + +

The best to do tracker

+ + + + + + \ No newline at end of file diff --git a/02week/todo.js b/02week/todo.js new file mode 100644 index 000000000..582ef0178 --- /dev/null +++ b/02week/todo.js @@ -0,0 +1,57 @@ +let addButton = document.getElementById("add"); +addButton.addEventListener('click', function(){ +//read the value from the input box + let inputElement = document.getElementById('inputText'); + let toDoText = inputElement.value; + inputElement.value = ''; + //create li element + let li = document.createElement("li"); + //create span element + let span = document.createElement("span"); + //update inner text of span element + span.innerText = toDoText; + //create delete button + let deleteButton = document.createElement('button'); + //update inner text of delete button + deleteButton.innerText = 'delete'; + //add class to the delete button + deleteButton.classList.add("delete"); + //add li to bottom of ul element + let ul = document.querySelector('ul'); + ul.appendChild(li); + //add span and delete button as children of newly created li + li.appendChild(span); + setupSpanEvent(span); + li.appendChild(deleteButton); + setupDeleteEvent(deleteButton); +}) + +let allDeletes = document.querySelectorAll(".delete"); +for(let i = 0; i < allDeletes.length; i++){ + let deleteButton = allDeletes[i]; + setupDeleteEvent(deleteButton); +} + +function setupDeleteEvent(deleteButton){ + deleteButton.addEventListener('click', function(){ + let parentLi = deleteButton.parentElement; + parentLi.remove(); + //let parentUl = parentLi.parentElement; + //parentUl.removeChild(parentLi); +}); +} + +let allSpans = document.querySelectorAll("span"); +for(let i = 0; i>> + /*let canDrink = []; + for(let i = 0; i < ages.length; i++){ + if(ages[i] >= 21){ + canDrink.push(ages[i]); + //adds the age to the new array + } + } + console.log(canDrink); + */ + + + /*const canDrink = ages.filter(function(age){ + if(age >= 21){ + return true; + } + }); + console.log(canDrink); + */ + //3 different ways to do the same thing + const canDrink = ages.filter(age => age >= 21); + console.log(canDrink); + + //<<<======= filter =======>>> + + //retail companies + /*const retail = companies.filter(function(company){ + if(company.category === "retail"){ + return true; + } + }); + console.log(retail); + */ //parameter + const retail = companies.filter(company => company.category === 'retail'); + console.log(retail); + + //Get 80s Companies + const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990)); + console.log(eightiesCompanies); + + //Companies that lasted 10 years plus + const TenYearCompanies = companies.filter(company =>(company.end - company.start >= 10)); + + console.log(TenYearCompanies); + + //<<<===== MAP ======>>> + + //create array of company names + const companyNames = companies.map(function(company){ + return company.name; + }); + console.log(companyNames); + + /*const test = companies.map(function(company){ + return '${company.name}[$(company.start) - ${company.end}]'; + }); + console.log(test);*/ + +//const testMap = companies.map(company => '${company.name} [${company.start} - ${company.end}]'); +//console.log(testMap); + +const agesSquare = ages.map(age => Math.sqrt(age)); +console.log(agesSquare) + +const agesTimesTwo = ages.map(age => age * 2); +console.log(agesTimesTwo); + + +//Square a number and then multiply it by two + +const agesSqrtTimesTwo = ages + .map(age => Math.sqrt(age)) + .map(age => age * 2); + + console.log(agesSqrtTimesTwo); + + //<<<====== SORT ======>>> +//sort companies by start year + /*const sortedCompanies = companies.sort(function(c1, c2) { + if(c1.start > c2.start){ + return 1; + }else { + return -1; + } + }); +console.log(sortedCompanies); +*/ + +const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1)); +console.log(sortedCompanies); + +//ascending order +const sortAges = ages.sort((a,b) => a - b); +console.log(sortAges); + +//for descending order b - a + +//<<<====== REDUCE =======>>> +//reduces the array to one single value, an iteration method. Uses a callback function that +//you provide to iterate through elements in the array and gradually combine their values into +// a final return value. + +/*let ageSum = 0; +for(let i = 0; i < ages.length; i++){ +ageSum = ageSum + ages[i]; +} +console.log(ageSum); +*/ + +/*const ageSum = ages.reduce(function(total, age){ + return total + age; +}, 0 ); +console.log(ageSum);*/ + +const ageSum = ages.reduce((total,age) => total + age, 0); +console.log(ageSum); + +//get total years for all companies + +/*const totalYears = companies.reduce(function(total, company){ + return total + (company.end - company.start); +}, 0); + +console.log(totalYears); +*/ + +const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0); +console.log(totalYears); + +//<<<===== COMBINE METHODS =====>>> + +/* +const combined = ages; + .map(age => age * 2) + .filter(age => age >= 35) + .sort((a, b) => a - b) + .reduce((a, b) => a + b, 0); + + console.log(combined); +*/ \ No newline at end of file diff --git a/03week/highOrderFunctions2.html b/03week/highOrderFunctions2.html new file mode 100644 index 000000000..93cde8c56 --- /dev/null +++ b/03week/highOrderFunctions2.html @@ -0,0 +1,19 @@ + + + + + Document + + + +

Array 2

+ + + \ No newline at end of file diff --git a/03week/highOrderFunctions2.js b/03week/highOrderFunctions2.js new file mode 100644 index 000000000..ff2c97ab6 --- /dev/null +++ b/03week/highOrderFunctions2.js @@ -0,0 +1,42 @@ +const skateShops = [ + {name: "No Comply", state: "TX", start: 2010, end: 2020}, + {name: "Supreme", state: "NY", start: 1992, end: 2022}, + {name: "Overtime", state: "TX", start: 2018, end: 2018} +]; + +const boardsSold = [10, 20, 25, 27, 30]; + +//forEach + +skateShops.forEach(function(company){ + console.log(company.name); +}); + +//FILTER +const boards = boardsSold.filter(function(sold){ + if(sold > 20){ + return true; + } +}); +//shorthand version of above function +//const boards = boardsSold.filter(sold => sold > 20); +console.log(boards); + +/*const texasShop = skateShops.filter(function(shops){ + if (shops.state === "TX"){ + return true; + } +});*/ + +const texasShop = skateShops.filter(shops => shops.state === "TX"); +console.log(texasShop); + +const ninetiesCompanies = skateShops.filter(shops => (shops.start >= 1990 && shops.start < 2000)); +console.log(ninetiesCompanies); +//map +//sort +//reduce +const greeting = function greet(name){ + return "Hi, im " + name; +} +console.log(greeting("Ben")); \ No newline at end of file diff --git a/03week/index.html b/03week/index.html new file mode 100644 index 000000000..a746d470b --- /dev/null +++ b/03week/index.html @@ -0,0 +1,13 @@ + + + + + Document + + +

Array

+ + + + + \ No newline at end of file diff --git a/03week/main.js b/03week/main.js new file mode 100644 index 000000000..9db114312 --- /dev/null +++ b/03week/main.js @@ -0,0 +1,153 @@ +const companies = [ + {name: "Howard Edward", category: "Grocery", start: 1980, end: 2020}, + {name: "Dell", category: "Tech", start: 1981, end: 1990}, + {name: "Nike", category: "retail", start: 1975, end: 2020 } +]; + +const ages = [33, 34, 35, 20]; + +/*for(let i = 0; i < companies.length; i++){ + console.log(companies[i]); +}*/ +//forEach efficient way to loop through an array, doesn't return anything back to you + +companies.forEach(function(company) { + console.log(company.name); + }) + + //<<<===== Filter =====>>> + /*let canDrink = []; + for(let i = 0; i < ages.length; i++){ + if(ages[i] >= 21){ + canDrink.push(ages[i]); + //adds the age to the new array + } + } + console.log(canDrink); + */ + + + /*const canDrink = ages.filter(function(age){ + if(age >= 21){ + return true; + } + }); + console.log(canDrink); + */ + //3 different ways to do the same thing + const canDrink = ages.filter(age => age >= 21); + console.log(canDrink); + + //<<<======= filter =======>>> + + //retail companies + /*const retail = companies.filter(function(company){ + if(company.category === "retail"){ + return true; + } + }); + console.log(retail); + */ //parameter + const retail = companies.filter(company => company.category === 'retail'); + console.log(retail); + + //Get 80s Companies + const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990)); + console.log(eightiesCompanies); + + //Companies that lasted 10 years plus + const TenYearCompanies = companies.filter(company =>(company.end - company.start >= 10)); + + console.log(TenYearCompanies); + + //<<<===== MAP ======>>> + + //create array of company names + const companyNames = companies.map(function(company){ + return company.name; + }); + console.log(companyNames); + + /*const test = companies.map(function(company){ + return '${company.name}[$(company.start) - ${company.end}]'; + }); + console.log(test);*/ + +//const testMap = companies.map(company => '${company.name} [${company.start} - ${company.end}]'); +//console.log(testMap); + +const agesSquare = ages.map(age => Math.sqrt(age)); +console.log(agesSquare) + +const agesTimesTwo = ages.map(age => age * 2); +console.log(agesTimesTwo); + + +//Square a number and then multiply it by two + +const agesSqrtTimesTwo = ages + .map(age => Math.sqrt(age)) + .map(age => age * 2); + + console.log(agesSqrtTimesTwo); + + //<<<====== SORT ======>>> +//sort companies by start year + /*const sortedCompanies = companies.sort(function(c1, c2) { + if(c1.start > c2.start){ + return 1; + }else { + return -1; + } + }); +console.log(sortedCompanies); +*/ + +const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1)); +console.log(sortedCompanies); + +//ascending order +const sortAges = ages.sort((a,b) => a - b); +console.log(sortAges); + +//for descending order b - a + +//<<<====== REDUCE =======>>> +/*let ageSum = 0; +for(let i = 0; i < ages.length; i++){ +ageSum = ageSum + ages[i]; +} +console.log(ageSum); +*/ + +/*const ageSum = ages.reduce(function(total, age){ + return total + age; +}, 0 ); +console.log(ageSum);*/ + +const ageSum = ages.reduce((total,age) => total + age, 0); +console.log(ageSum); + +//get total years for all companies + +/*const totalYears = companies.reduce(function(total, company){ + return total + (company.end - company.start); +}, 0); + +console.log(totalYears); +*/ + +const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0); +console.log(totalYears); + +//<<<===== COMBINE METHODS =====>>> + +/* +const combined = ages; + .map(age => age * 2) + .filter(age => age >= 35) + .sort((a, b) => a - b) + .reduce((a, b) => a + b, 0); + + console.log(combined); +*/ \ No newline at end of file diff --git a/03week/ticTacToe.html b/03week/ticTacToe.html new file mode 100644 index 000000000..81cf40e71 --- /dev/null +++ b/03week/ticTacToe.html @@ -0,0 +1,13 @@ + + + + + Document + + + + + + + + \ No newline at end of file diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..2e67391d9 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -21,31 +21,91 @@ function printBoard() { console.log('1 ' + board[1].join(' | ')); console.log(' ---------'); console.log('2 ' + board[2].join(' | ')); -} +} +//should return true if the player won on any row function horizontalWin() { - // Your code here + // Your code here //use loop + for(let i = 0; i <=2; i++){ + if(board[i][0] === 'X' && board[i][1] === 'X' && board[i][2]==='X'){ + return true; + }else if(board[i][0] === 'O' && board[i][1]=== 'O' && board[i][2]=== 'O'){ + return true; + } + } - +} +//should return true, if the player won on any column function verticalWin() { - // Your code here + for(let i = 0; i <= 2; i++){ + if(board[0][i] ==='X' && board[1][i] ==='X' && board[2][i] === 'X'){ + return true; + }else if(board[0][i] === 'O' && board[1][i] ==='O' && board[2][i] === 'O'){ + return true; + } + // Your code here //use loop } - +} +//should return true if the player won diagnol function diagonalWin() { + if(board[0][0]==='X' && board[1][1]==='X' && board[2][2]==='X'){ + return true; + }else if(board[0][0]==='O' && board[1][1]==='O' && board[2][2]==='O'){ + return true; + }else if(board[0][2]==='X' && board[1][1]==='X' && board[2][0]==='X'){ + return true; + }else if(board[0][2]==='O' && board[1][1]==='O' && board[2][0]==='O'){ + return true; + }else{ + return false;//???????? + } // Your code here } - +//should return true if any of the top three functions return true function checkForWin() { + //may need if/else if... + if (horizontalWin() || verticalWin() || diagonalWin()){ + return true; + }else { + return false; + } // Your code here } function ticTacToe(row, column) { + board[row][column] = playerTurn; + if(playerTurn === 'X'){ + playerTurn = 'O'; + //then assign playerturn to O after X is assigned to a square + }else { + playerTurn = 'X'; + } + // if playerturn O is found assign playerTurn to X + // Your code here +//set the value on the box +//check if the player won +//if they did (say congrats!! +//switch out the variable for the other player + } +/*function filledSpace(row, column){ + if(board[row][column] === 'X' || 'O'){ + console.log("Try another position") + }; + //check to see if space is filled + //if position is filled choose another column, row +}*/ + function getPrompt() { printBoard(); + if(checkForWin() === true){ + return console.log("Congrats, you win"); + }else { console.log("It's Player " + playerTurn + "'s turn."); + } + filledSpace(); rl.question('row: ', (row) => { rl.question('column: ', (column) => { ticTacToe(row, column); diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..061b8f044 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -19,23 +19,50 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { +function movePiece(startStack, endStack) { // Your code here + let elem = stacks[startStack].pop(); + stacks[endStack].push(elem); } -function isLegal() { +function isLegal(startStack, endStack) { // Your code here - +//is position greater or less than what peice you're trying to put on top of other +let elem1 = [stacks[startStack].length-1]; +//if element 1(startstack) is greater than zero, and element2(endstack) +//is equal to zero return true +let elem2 = stacks[endStack]; +//if element 1 has value and element 2 has no value, you can place a value there + if(elem1 > 0 && elem2.length == 0){ + return true; + + }if (elem2 > elem1){ + return true; + + } else{ + return false; + } } -function checkForWin() { +function checkForWin(startStack, endStack) { // Your code here - + //if numbers are in descending order then you win + //if stack b is full return true + //if length of stack b is equal to number of pieces expected, you win + if(stacks.b.length === 4){ + console.log("Winner!!") + return true; + }else { + return false; + } } function towersOfHanoi(startStack, endStack) { - // Your code here + // Your code here] + movePiece(startStack, endStack); + //isLegal(startStack, endStack); + //checkForWin(startStack, endStack); } diff --git a/04week/hiOrderFunc b/04week/hiOrderFunc new file mode 100644 index 000000000..494500796 --- /dev/null +++ b/04week/hiOrderFunc @@ -0,0 +1,21 @@ +i = 0; +do{ +i++; +console.log(i) +}while(i >= 0 && i <= 1000) + +const Person = { + firstName: "Jane", + lastName: "Doe", + birthDate: "Jan 5, 1925", + gender: "female" +}; + +//Use a for...in loop and if statement to console.log the value associated +//with the key birthDate if the birth year is an odd number. + +for (const property in Person){ + if(Person.birthDate % 3 === 0){ + console.log(Person.birthDate); + } +} \ No newline at end of file diff --git a/04week/hiOrderFunc.js b/04week/hiOrderFunc.js new file mode 100644 index 000000000..ee6b6e40c --- /dev/null +++ b/04week/hiOrderFunc.js @@ -0,0 +1,97 @@ +//Use a do...while loop to console.log the numbers from 1 to 1000. + +i = 0; +do{ +i++; +console.log(i) +}while(i > 0 && i <= 1000) + +/*Create an object (an array with keys and values) called person with the following data: +firstName: "Jane" +lastName: "Doe" +birthDate: "Jan 5, 1925" +gender: "female" +*/ + +const Person = { + firstName: "Jane", + lastName: "Doe", + birthDate: "Jan 5, 1925", + gender: "female" +}; + +//Use a for...in loop and if statement to console.log the value associated +//with the key birthDate if the birth year is an odd number. +//find diff way to find odd values + +for (const property in Person){ + if(property.birthDate % 3 === 0){ + console.log("for in "+ Person.birthDate); + } +} + +/*Create an arrayOfPersons that contains multiple objects. +You can simply copy/paste the person object you made above multiple times. +Feel free to change the values to reflect multiple people you might have in your database.*/ +var arrayOfPersons = [ +Person1 = { + firstName: "John", + lastName: "Smith", + birthDate: "Feb 15, 1985", + gender: "male" +}, + +Person2 = { + firstName: "Jim", + lastName: "Beam", + birthDate: "March 10, 1991", + gender: "male" +}, + +Person3 = { + firstName: "Anna", + lastName: "Bananna", + birthDate: "Feb 23, 1993", + gender: "female" +} +] + +console.log(arrayOfPersons); + +//Use .map() to map over the arrayOfPersons and console.log() their information. + +function getObject(item){ + const fullObject = [item.firstName, item.lastName, item.birthDate, item.gender]; + return fullObject; +} + console.log(arrayOfPersons.map(getObject)); + + + //Use .filter() to filter the persons array and console.log only males in the array. + +var newArray = arrayOfPersons.filter(function(el){ + if (el.gender === "male"){ + return true; + //keep in list + }else{ + return false; + //take out of list + } +}); +console.log(newArray); + +//Use .filter() to filter the persons array and console.log only people that were born before Jan 1, 1990. + +var birthday = arrayOfPersons.filter(function(person){ + date = new Date("Jan 1, 1990") + if (new Date(person.birthDate) < date){ + return true; + }else { + return false; + } + //Date.parse() || new Date() + //will give you the date object + //call get year method will return year for date + //return true or false +}); +console.log(birthday); \ No newline at end of file diff --git a/04week/mastermind.js b/04week/mastermind.js index 60e5cfa18..72d93b3b7 100644 --- a/04week/mastermind.js +++ b/04week/mastermind.js @@ -14,6 +14,8 @@ let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; function printBoard() { for (let i = 0; i < board.length; i++) { console.log(board[i]); + //guess.push(); + //hint.push(); } } @@ -28,13 +30,75 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { +function generateHint(guess) { // your code here -} + let correctLetters = 0; + let correctLetterLocations = 0; + let solutionArray = solution.split(''); + let guessArray = guess.split(''); + //splitting on empting string for solution and guess + + //console.log(solutionArray); + //for(let i = 0; i < solutionArray.length; i++){ + guessArray.forEach((item, index) => { + if(item === solutionArray[index]){ + correctLetterLocations += 1; + solutionArray[index] = null; + guessArray[index] = null; + } + }); + //if(solutionArray[i] === guessArray[i]){ + //if solutionArray index is equal to guessArray index + //correctLetterLocations +=1; + //incremenet correctLetterLocations (comparing solution against guess) + //solutionArray[i] = null; + //guessArray[i] = null; + //set solutionArray index to null (if solution and guess match set to "null") + + + //console.log(solutionArray) + + //for(let i = 0; i < solutionArray.length; i++){ + guessArray.forEach((item, index) => { + // if(solutionArray[i] === null){ + if(item === null){ + //if null returns -1 + let targetIndex = solutionArray.indexOf(item); + + //let targetIndex = guessArray.indexOf(solutionArray[i]); + //deteremine if current index in guessArray appears inside of solutionArray + //if you dont find correctLetter "if(targetIndex > -1)" + //console.log(targetIndex); + if(targetIndex > -1){ + correctLetters += 1; + solutionArray[targetIndex] = null; + guessArray[index]= null; + } + } + }); +console.log(`${correctLetterLocations} - ${correctLetters}`) +return `${correctLetterLocations}-${correctLetters}`; + } + function mastermind(guess) { solution = 'abcd'; // Comment this out to generate a random solution // your code here + let hint = generateHint(guess); + board.push(guess); + + //console.log(`${board.push(guess)} ${board.push(hint)}`); +if(guess === solution){ + //if guess is equal to solution + console.log('You guessed it!'); + return 'You guessed it!'; +} + if(board.length >= 10){ + console.log('You ran out of turns! The solution was ' + solution); +}else{ + console.log('Guess again.'); +} + } diff --git a/04week/mastermindalt.js b/04week/mastermindalt.js new file mode 100644 index 000000000..7e5e38c58 --- /dev/null +++ b/04week/mastermindalt.js @@ -0,0 +1,116 @@ +'use strict'; + +const assert = require('assert'); +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +let board = []; +let solution = ''; +let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; + +function printBoard() { + for (let i = 0; i < board.length; i++) { + console.log(board[i]); + //guess.push(); + //hint.push(); + } +} + +function generateSolution() { + for (let i = 0; i < 4; i++) { + const randomIndex = getRandomInt(0, letters.length); + solution += letters[randomIndex]; + } +} + +function getRandomInt(min, max) { + return Math.floor(Math.random() * (max - min)) + min; +} + +function generateHint(guess) { + let correctLetterLocations = 0, + correctLetters = 0; + let solutionArray = solution.split(""); + let guessArray = guess.split(""); + guessArray.forEach((item, index) => { + if (item === solutionArray[index]) { + correctLetterLocations += 1; + solutionArray[index] = null; + guessArray[index] = null; + } + }); + guessArray.forEach((item, index) => { + if (item === null) { + let findIndex = solutionArray.indexOf(item); + if (findIndex > -1) { + correctLetters += 1; + solutionArray[findIndex] = null; + guessArray[index] = null; + } + } + }); + console.log(`${correctLetterLocations}-${correctLetters}`); + return `${correctLetterLocations}-${correctLetters}`; +} + +function mastermind(guess) { + solution = 'abcd'; // Comment this out to generate a random solution + // your code here + let hint = generateHint(guess); + board.push(guess); + + //console.log(`${board.push(guess)} ${board.push(hint)}`); +if(guess === solution){ + //if guess is equal to solution + console.log('You guessed it!'); + return 'You guessed it!'; +} + if(board.length >= 10){ + console.log('You ran out of turns! The solution was ' + solution); +}else{ + console.log('Guess again.'); +} + +} + + +function getPrompt() { + rl.question('guess: ', (guess) => { + mastermind(guess); + printBoard(); + getPrompt(); + }); +} + +// Tests + +if (typeof describe === 'function') { + solution = 'abcd'; + describe('#mastermind()', () => { + it('should register a guess and generate hints', () => { + mastermind('aabb'); + assert.equal(board.length, 1); + }); + it('should be able to detect a win', () => { + assert.equal(mastermind(solution), 'You guessed it!'); + }); + }); + + describe('#generateHint()', () => { + it('should generate hints', () => { + assert.equal(generateHint('abdc'), '2-2'); + }); + it('should generate hints if solution has duplicates', () => { + assert.equal(generateHint('aabb'), '1-1'); + }); + + }); + +} else { + + generateSolution(); + getPrompt(); +} \ No newline at end of file diff --git a/05week/bankAccount.js b/05week/bankAccount.js new file mode 100644 index 000000000..78466b0a5 --- /dev/null +++ b/05week/bankAccount.js @@ -0,0 +1,139 @@ +//For this assignment we are creating 2 classes that can be used to represent a Bank Account and the transactions it holds. + + +/*BankAccount Class - This class represents a Bank Account +----------------- +1. The class should have the following fields: + a. accountNumber - String representing the account number + b. owner - String representing the owner of the account + c. transactions - an array of transactions representing the history of all transactions associated with this account +*/ +/* +2. The constructor should take in the following input: + a. accountNumber - the account number + b. owner - this is the name of the person who owns the account + c. NOTE: that the when an account is created, you should initialize the transactions array to be an empty array. +*/ + +class BankAccount { + constructor(accountNumber, owner){ + this.accountNumber = accountNumber; + this.owner = owner; + this.transactions = []; + this.accountBalance = 0; + + //NOTE: that the when an account is created, you should initialize the transactions array to be an empty array. + } + balance(){ + return this.accountBalance; + } + + deposit(amt){ + if(amt >= 0){ + this.accountBalance = this.accountBalance + amt + return this.transactions.push(this.accountBalance); + } else { + return false; + } + } + + charge(payee, amt){ + if(amt < this.accountBalance){ + this.accountBalance = this.accountBalance - amt; + return this.transactions.push(payee, amt); + } else { + return false; + } + } +} + +/* +3. The class should have the following 3 methods: + a. balanace() - this method does not take any input, and returns the current balance on the account. The balance is computed + by summing up the amounts in the transactions array + b. deposit(amt) - this method takes in a single input, the deposit amount. This method should create a new transaction + representing the deposit, and add it to the transactions array. + - You should not be able to deposit a negative amount + c. charge(payee, amt) - this method takes in the payee and amount, creates a new transaction with the payee and amount, and + adds the transaction to the transaction array + - You should not be able to charge an amount that would make you balance dip below 0 + +**/ + +/*Transaction Class - This class represents a single transaction in a Bank Account +----------------- +1. The class should have the following fields: + a. date - the date of the transaction + b. amount - the amount of the transaction. Positive amounts are money going into the account (deposit, refund). Negative amounts are money coming out of the account (a charge, or debit) + c. payee - the description or payee on the transaction + +2. The constructor should take in the following input + a. amount - the amount on the transaction + b. payee - the payee or description on the transaction + c. NOTE: that the date is not passed into the constructor. the constructor should set the date to be the current date automatically +*/ + +class Transaction { + constructor(amount, payee){ + this.amount = amount; + this.payee = payee; + this.date = Date(); + } +} + +class SavingsAccount extends BankAccount { + constructor(accountNumber, owner, interestRate){ + super(accountNumber, owner, interestRate) + this.interestRate = .4; + } + + + accrueInterest(){ + let interest = balance() * this.interestRate; + return this.transactions.push(interest); + } +} + +/* +Stretch Goal: SavingsAccount Class - This class should extend the BankAccount Class +---------------------------------- +1. The class should have an additional field: + a. interestRate - this value represents the rate at which the account earns interest + +2. The constructor should take the following as input: + a. accountNumber - see BankAccount class + b. owner - see BankAccount class + c. interestRate - the rate that is used to compute interest + +3. Additional methods: + a. accrueInterest() - this method should use the balance() to get the current balance, and add a new transaction representing a deposit of the appropriate amount. + + + + +Example Usage - You can turn these into tests + + +let acct1 = new BankAccount("5553429", "John Doe"); + +console.log(acct1.accountNumber); // 5553429 +console.log(acct1.owner); // John Doe +console.log(acct1.balance()): // 0 + +acct1.deposit(100) +console.log(acct1.balance()): // 100 + +acct1.deposit(-200) // should not be allowed +console.log(acct1.balance()): // 100 + +acct1.charge("Target", 30.50) +acct1.charge("FreeBirds", 15.15) +console.log(acct1.balance()) //54.35 + +acct1.charge("Diamond Shop", 1000) // should not be allowed +console.log(acct1.balance()) //54.35 + +acct1.charge("Targe", -20) //refund +console.log(acct1.balance()) //74.35 + +*/ \ No newline at end of file diff --git a/05week/spaceTravelToMars.js b/05week/spaceTravelToMars.js index ce258a382..7d96015cf 100644 --- a/05week/spaceTravelToMars.js +++ b/05week/spaceTravelToMars.js @@ -11,6 +11,58 @@ let jobTypes = { // Your code here +//you wnat CrewMember class +// constructor should take in as input: name, their job, their specialSkill +// have an attribute "ship", this is the ship they are in + +class CrewMember { + constructor(name, job, specialSkill){ + this.name = name; + this.job = job; + this.specialSkill = specialSkill; + this.ship = null; + } + +//this method should ass THIS crewmember to the ship being passed in +//NOTE: an entire ship instance is passed in, not just the name +//NOTE: the entire crewmember iS ADDED + enterShip(someShip){ + this.ship = someShip; + someShip.crew.push(this); + } + } + +class Ship { + constructor(name, type, ability){ + this.name = name; + this.type = type; + this.ability = ability; + this.crew = []; +//have a list of crew that starts out empty "crew = [];" + } +//this method should return the ship's ability if there is a crew +//whose job matches up with the ships type +//otherwise should return "Cant perform a mission statement yet." + missionStatement(){ + if(this.crew.length === 0){ + return "Can't perform a mission yet." + } + else { + return this.ability; + } + } +} + + +//you want a ship class +//constructor should take in as input: name, type, ability +//have a list of crew, that starts out empty +//ship should have a "missionStatement()", +//if there is a crew member that can activate it, it should return the ships ability +//otherwise it should return "Can't perform a mission yet." + + + //tests if (typeof describe === 'function'){ describe('CrewMember', function(){ diff --git a/06week/README.md b/06week/README.md new file mode 100644 index 000000000..1d9077bdd --- /dev/null +++ b/06week/README.md @@ -0,0 +1,5 @@ +Created an array of people to choose from of eligible players, with different attributes for each of them. +Created different classes to hold the attributes and move them from array to array, and display them in the DOM. Created a listPeopleChoices function to create new players and map through the array of people. Creates a button that allows you to make a player that when clicked moves the object to the listOfPlayers array. Once listOfPlayers is created they are given two button options for the red or blue team which when clicked will move them to the selected array. +step 1: Array of People +step 2: ARRAY OF PLAYERS +STEP 3: array of red or blue team diff --git a/06week/dodgeball.html b/06week/dodgeball.html new file mode 100644 index 000000000..867db27ae --- /dev/null +++ b/06week/dodgeball.html @@ -0,0 +1,29 @@ + + + + + + + Dodge Ball + + +
+

List Of People

+ +
+ +
+

Dodge Ball Players

+ +
+
+

Blue Team

+ +
+
+

Red Team

+ +
+ + + \ No newline at end of file diff --git a/06week/dodgeball.js b/06week/dodgeball.js new file mode 100644 index 000000000..64a69ebc0 --- /dev/null +++ b/06week/dodgeball.js @@ -0,0 +1,202 @@ +const arrOfPeople = [ + { + id: 2, + name: "Charles Young", + age: 55, + skillSet: "welding", + placeBorn: "Omaha, Nebraska" + }, + { + id: 3, + name: "Judy Twilight", + age: 35, + skillSet: "fishing", + placeBorn: "Louisville, Kentucky" + }, + { + id: 4, + name: "Cynthia Doolittle", + age: 20, + skillSet: "tic tac toe", + placeBorn: "Pawnee, Texas" + }, + { + id: 5, + name: "John Willouby", + age: 28, + skillSet: "pipe fitting", + placeBorn: "New York, New York" + }, + { + id: 6, + name: "Stan Honest", + age: 20, + skillSet: "boom-a-rang throwing", + placeBorn: "Perth, Australia" + }, + { + id: 7, + name: "Mia Watu", + age: 17, + skillSet: "acrobatics", + placeBorn: "Los Angeles, California" + }, + { + id: 8, + name: "Walter Cole", + age: 32, + skillSet: "jump rope", + placeBorn: "New Orleans, Louisiana" + }, +] +//take list of people and create new player instances +//there should NOT be two teammate classes only one not blueTeammate and redTeammate +//teammate class which represents a player once they get selected for a team +//attribute of teammate class should be teamname all teammates have an attribute called team name +//if you blue your attribute will say blue if youre red your attribute will say red +const listOfPlayers = [] +const blueTeam = [] +const redTeam = [] + +class player { + constructor(id, name, age, skillSet, placeBorn){ + this.id = id; + this.name = name; + this.age = age; + this.skillSet = skillSet; + this.placeBorn = placeBorn; + + //listPeopleChoices + } +} + +//the parent class is player, the child is teammate +class Teammate extends player { + constructor(id, name, age, skillSet, placeBorn, teamName){ + super(id, name, age, skillSet, placeBorn);//super calls player constructor + this.teamName = teamName; + /*if your a blue teammate you attribute will say blue, + if you a red teammamte your attribute will say red*/ + } +} +//teammate class should extend the player class +//Ex: truck extends vehicle +//every teammate has the same attributes of player plus one more +//which is which team theyre on +const listPeopleChoices = () => { + const listElement = document.getElementById('people') + arrOfPeople.map(person => { + //loops through arrOfPeople array + //creates list item tag + const li = document.createElement("li") + //creates button element + const button = document.createElement("button") + //text in button reads make player + button.innerHTML = "Make Player" + //when the button is clicked... + button.addEventListener('click', function() {makePlayer(person.id)} ) + li.appendChild(button) + li.appendChild(document.createTextNode(person.name + " - " + person.skillSet)) + listElement.append(li) + }) +} + +const makePlayer = (id) => { + let playerElement = arrOfPeople.find(function(player){ + //find players with "id" in arrOfPeople + return player.id == id; + +}); + //goes through index of arrOfPeople with id + let index = arrOfPeople.indexOf(playerElement); + //takes out selected value + arrOfPeople.splice(index, 1); + //removes 1 item from arrayOfPeople array + let newPlayer = new player (playerElement.id, playerElement.name, playerElement.age, playerElement.skillSet, playerElement.placeBorn); + //creates a new player object + //adds newPlayer object to listOfPlayers array + listOfPlayers.push(newPlayer); + //calls PlayerDisplay function from below + //PlayerDisplay(); + console.log(newPlayer) + PlayerDisplay(); + }; + +const makeBluePlayer = (player) => { + + blueTeam.push(player); + //push value into blueTeam array + + let bluePlayer = listOfPlayers.indexOf(player); + //index of player in listOfPlayers array + //the indexOf listOfPlayers array is assigned to bluePlayer + //takes out one item, bluePlayer value from listOfPlayers array + listOfPlayers.splice(bluePlayer, 1) + document.getElementById("blue").innerHTML = ""; + PlayerDisplay(); + const blueEl = document.getElementById('blue') + //loops (map) through blueTeam array + blueTeam.map(players => { + const li = document.createElement("li"); + li.appendChild(document.createTextNode(`${players.name} - ${players.id}`)) + blueEl.append(li); + }) + }; + + const makeRedPlayer = (player) => { + //push value into redTeam array + redTeam.push(player); + //indexOf player in listOfPlayers array is assigned to redPlayer + const redPlayer = listOfPlayers.indexOf(player); + //take out 1 item, redPlayer of listOfPlayers array + listOfPlayers.splice(redPlayer, 1) + document.getElementById("red").innerHTML = ""; + PlayerDisplay(); + const redEl = document.getElementById('red'); + + + //loops (map) through redTeam array + redTeam.map(players => { + const li = document.createElement("li"); + li.appendChild(document.createTextNode(`${players.name} - ${players.id}`)) + redEl.append(li); + }) + }; + + const PlayerDisplay = () => { + const listElement = document.getElementById('players') + listElement.innerHTML = ""; + //finds the id player and assigns it to list element + listOfPlayers.map(person => { + //loops through listOfPlayers array + //creates "li" tag + const li = document.createElement("li") + //li.appendChild(document.createTextNode(`${person.name} - ${person.skillSet}`)) + listElement.append(li) + document.getElementById("people").innerHTML = ""; + listPeopleChoices(); + //creates button + const button1 = document.createElement("button") + //creates button + const button2 = document.createElement("button") + //text in "button1" reads blue team + button1.innerHTML = "Blue Team" + // text in "button2" reads read team + button2.innerHTML = "Red Team" + //when button1 is clicked calls makeBluePlayer function + button1.addEventListener('click', function() {makeBluePlayer(person)} ) + li.appendChild(button1) + //li.appendChild(document.createTextNode(person.id + " - " + person.name)) + listElement.append(li) + //when button2 is clicked calls makeRedPlayer function + button2.addEventListener('click', function() {makeRedPlayer(person)} ) + li.appendChild(button2) + li.appendChild(document.createTextNode(person.name + " - " + person.age)) + listElement.append(li) + }) + }; +//var judy = new player (3, "Judy Twilight", 35, "fishing", "Louisville, Kentucky") +//console.log(judy); + + /*if your a blue teammate you attribute will say blue, + if you a red teammamte your attribute will say red*/ diff --git a/06week/sorting.js b/06week/sorting.js new file mode 100644 index 000000000..9af7cbf11 --- /dev/null +++ b/06week/sorting.js @@ -0,0 +1,130 @@ +const strNums = ["1","4","1","5","9","2","6","5","3","5","8","9","7","9","3","2","3","8","4","6","2","6","4","3","3","8","3","2","7","9","5","0","2","8","8","4","1","9","7","1","6","9","3","9","9","3","7","5","1","0","5","8","2","0","9","7","4","9","4","4","5","9","2","3","0","7","8","1","6","4","0","6","2","8","6","2","0","8","9","9","8","6","2","8","0","3","4","8","2","5","3","4","2","1","1","7","0","6","7","9","8","2","1","4","8","0","8","6","5","1","3","2","8","2","3","0","6","6","4","7","0","9","3","8","4","4","6","0","9","5","5","0","5","8","2","2","3","1","7","2","5","3","5","9","4","0","8","1","2","8","4","8","1","1","1","7","4","5","0","2","8","4","1","0","2","7","0","1","9","3","8","5","2","1","1","0","5","5","5","9","6","4","4","6","2","2","9","4","8","9","5","4","9","3","0","3","8","1","9","6","4","4","2","8","8","1","0","9","7","5","6","6","5","9","3","3","4","4","6","1","2","8","4","7","5","6","4","8","2","3","3","7","8","6","7","8","3","1","6","5","2","7","1","2","0","1","9","0","9","1","4","5","6","4","8","5","6","6","9","2","3","4","6","0","3","4","8","6","1","0","4","5","4","3","2","6","6","4","8","2","1","3","3","9","3","6","0","7","2","6","0","2","4","9","1","4","1","2","7","3","7","2","4","5","8","7","0","0","6","6","0","6","3","1","5","5","8","8","1","7","4","8","8","1","5","2","0","9","2","0","9","6","2","8","2","9","2","5","4","0","9","1","7","1","5","3","6","4","3","6","7","8","9","2","5","9","0","3","6","0","0","1","1","3","3","0","5","3","0","5","4","8","8","2","0","4","6","6","5","2","1","3","8","4","1","4","6","9","5","1","9","4","1","5","1","1","6","0","9","4","3","3","0","5","7","2","7","0","3","6","5","7","5","9","5","9","1","9","5","3","0","9","2","1","8","6","1","1","7","3","8","1","9","3","2","6","1","1","7","9","3","1","0","5","1","1","8","5","4","8","0","7","4","4","6","2","3","7","9","9","6","2","7","4","9","5","6","7","3","5","1","8","8","5","7","5","2","7","2","4","8","9","1","2","2","7","9","3","8","1","8","3","0","1","1","9","4","9","1","2","9","8","3","3","6","7","3","3","6","2","4","4","0","6","5","6","6","4","3","0","8","6","0","2","1","3","9","4","9","4","6","3","9","5","2","2","4","7","3","7","1","9","0","7","0","2","1","7","9","8","6","0","9","4","3","7","0","2","7","7","0","5","3","9","2","1","7","1","7","6","2","9","3","1","7","6","7","5","2","3","8","4","6","7","4","8","1","8","4","6","7","6","6","9","4","0","5","1","3","2","0","0","0","5","6","8","1","2","7","1","4","5","2","6","3","5","6","0","8","2","7","7","8","5","7","7","1","3","4","2","7","5","7","7","8","9","6","0","9","1","7","3","6","3","7","1","7","8","7","2","1","4","6","8","4","4","0","9","0","1","2","2","4","9","5","3","4","3","0","1","4","6","5","4","9","5","8","5","3","7","1","0","5","0","7","9","2","2","7","9","6","8","9","2","5","8","9","2","3","5","4","2","0","1","9","9","5","6","1","1","2","1","2","9","0","2","1","9","6","0","8","6","4","0","3","4","4","1","8","1","5","9","8","1","3","6","2","9","7","7","4","7","7","1","3","0","9","9","6","0","5","1","8","7","0","7","2","1","1","3","4","9","9","9","9","9","9","8","3","7","2","9","7","8","0","4","9","9","5","1","0","5","9","7","3","1","7","3","2","8","1","6","0","9","6","3","1","8","5","9","5","0","2","4","4","5","9","4","5","5","3","4","6","9","0","8","3","0","2","6","4","2","5","2","2","3","0","8","2","5","3","3","4","4","6","8","5","0","3","5","2","6","1","9","3","1","1","8","8","1","7","1","0","1","0","0","0","3","1","3","7","8","3","8","7","5","2","8","8","6","5","8","7","5","3","3","2","0","8","3","8","1","4","2","0","6","1","7","1","7","7","6","6","9","1","4","7","3","0","3","5","9","8","2","5","3","4","9","0","4","2","8","7","5","5","4","6","8","7","3","1","1","5","9","5","6","2","8","6","3","8","8","2","3","5","3","7","8","7","5","9","3","7","5","1","9","5","7","7","8","1","8","5","7","7","8","0","5","3","2","1","7","1","2","2","6","8","0","6","6","1","3","0","0","1","9","2","7","8","7","6","6","1","1","1","9","5","9","0","9","2","1","6","4","2","0","1","9","8","9"]; + +// Given 1000 digits of PI as strings, return an array of the digits as numbers +//use map function +const stringsToNumbs = (numbers) => { + const toNum = parseInt(strNums[numbers]); +} +console.log(stringsToNumbs()); +// With the same numbers, find the sum of the even values +//use reduce and filter hi order functions +const sumEvens = (numbers) => { + let strToInt = numbers.map(x => parseInt(x)) + let evenArray = strToInt.filter(x => x % 2 === 0); + let total = evenArray.reduce((t, n) => t + n) + return total + } + +console.log(sumEvens(strNums)); + +// Find the index of the first value when added to it's index = 512 (#ATX!!) +const num = (numbers) => { + let strToInt = numbers.map(x => parseInt(x)) + //return strToInt + strToInt.forEach(function(item, index, array){ + //console.log(item, index) + if(item + index === 512){ + console.log("item: " + item, "index: " + index) + console.log(item + index) + }else{ + return false; + } + }) + } + console.log(num(strNums)); + +const weather = [ + { id: 5743823523151872, + weather_state_name: "Light Cloud", + weather_state_abbr: "lc", + wind_direction_compass: "NNE", + created: "2018-07-11T20:53:03.251710Z", + applicable_date: "2018-07-11", + min_temp: 14.43, + max_temp: 23.36, + the_temp: 22.785, + wind_speed: 5.682503989556987, + wind_direction: 21.6264939172659, + air_pressure: 1024.45, + humidity: 58, + visibility: 8.683041040324504, + predictability: 70 + }, + { id: 6188149969518592, + weather_state_name: "Heavy Cloud", + weather_state_abbr: "hc", + wind_direction_compass: "NE", + created: "2018-07-11T20:53:03.268190Z", + applicable_date: "2018-07-12", + min_temp: 14.81, + max_temp: 25.52, + the_temp: 24.61, + wind_speed: 3.2461141472739206, + wind_direction: 42.72552812997726, + air_pressure: 1024.605, + humidity: 54, + visibility: 10.633835898353615, + predictability: 71 + }, + { id: 5742049676492800, + weather_state_name: "Showers", + weather_state_abbr: "s", + wind_direction_compass: "E", + created: "2018-07-11T20:53:03.947390Z", + applicable_date: "2018-07-13", + min_temp: 15.5525, + max_temp: 25.3475, + the_temp: 24.175, + wind_speed: 3.6572546846814604, + wind_direction: 90.32910675612557, + air_pressure: 1025.385, + humidity: 57, + visibility: 10.181166984808717, + predictability: 73 + }, + { id: 6696130918219776, + weather_state_name: "Heavy Cloud", + weather_state_abbr: "hc", + wind_direction_compass: "SSW", + created: "2018-07-11T20:53:04.068570Z", + applicable_date: "2018-07-14", + min_temp: 15.915, + max_temp: 27.0925, + the_temp: 26.585, + wind_speed: 3.649847972759087, + wind_direction: 200.04283406736377, + air_pressure: 1024.4450000000002, + humidity: 52, + visibility: 11.14056410562316, + predictability: 71 + }, +]; + +//using a higher order function, create an array of the unique 'weather_state_name' values of the weather array. +//Your function should return the following array ['Light Cloud', 'Heavy Cloud', 'Showers'] +let weatherArray = []; +const weatherStates = weather.forEach(function(element){ + if(weatherArray.includes(element.weather_state_name)){ + //do nothing + //access the weather_state_name using the new array?? + }else { + weatherArray.push(element.weather_state_name) + //return weatherArray + } + //console.log(weatherArray) + //console.log(element.weather_state_name); + //ran in repl.it without console.log//// +}) +//put in new array and if it already exist in new array +console.log(weatherArray) + +//find the id of the object in weather that has a min_temp of 15.915 + +const idealTemp = weather.filter(function(min){ + if (min.min_temp === 15.915){ + return true; + }else{ + return false; + } +}); +console.log(idealTemp); diff --git a/07week/.vscode/settings.json b/07week/.vscode/settings.json new file mode 100644 index 000000000..cbb7b3af2 --- /dev/null +++ b/07week/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5502 +} \ No newline at end of file diff --git a/07week/addressAPI.html b/07week/addressAPI.html new file mode 100644 index 000000000..014301d85 --- /dev/null +++ b/07week/addressAPI.html @@ -0,0 +1,11 @@ + + + + + Document + + + + + + \ No newline at end of file diff --git a/07week/addressAPI.js b/07week/addressAPI.js new file mode 100644 index 000000000..07cc4d89f --- /dev/null +++ b/07week/addressAPI.js @@ -0,0 +1,8 @@ +const getPosts = () => { +return fetch(`https://randomuser.me/api/`) +.then(res => res.json()) +.then(res => console.log(res)) + +} + +console.log(getPosts()); diff --git a/07week/codeAlong.html b/07week/codeAlong.html new file mode 100644 index 000000000..6521e6451 --- /dev/null +++ b/07week/codeAlong.html @@ -0,0 +1,10 @@ + + + + + Document + + + + + \ No newline at end of file diff --git a/07week/codeAlong.js b/07week/codeAlong.js new file mode 100644 index 000000000..e69de29bb diff --git a/07week/promises.html b/07week/promises.html new file mode 100644 index 000000000..ace6ce6e3 --- /dev/null +++ b/07week/promises.html @@ -0,0 +1,28 @@ + + + + + + + Fetch Practice + + + + + + + + + + + +
+

All Posts

+ + + + +
+ + + \ No newline at end of file diff --git a/07week/promises.js b/07week/promises.js new file mode 100644 index 000000000..0d93346c6 --- /dev/null +++ b/07week/promises.js @@ -0,0 +1,61 @@ + +let arrayOfPosts; + +// this function waits for the web page to be loaded, when it does it will run the code inside of it which happen to be getPosts() +window.onload = function() { + getPosts() + +} + +// this function is going to make a fetch request to the url inside it's parameter brackets (). Then it will turn the response (data it's getting back), saved here as res. The res.json will not be saved as posts and saved into the variable, arrayOfPosts +const getPosts = () => { + fetch('http://jsonplaceholder.typicode.com/posts') + .then(res => res.json()) + .then(posts => arrayOfPosts = posts) +} + +// this function logs the results in your browsers console +const consolePosts = () => { + console.log(arrayOfPosts) +} + +// this function creates elements inside the all-posts ul, then appends text inside it with the posts that were returned in the request. +const displayPost = () => { + const allPosts = document.getElementById('all-posts') + arrayOfPosts.map((post, index) => { + const li = document.createElement('li') + const text = document.createTextNode(`#${index}, Title: ${post.title}: ${post.body}, by user: ${post.userId}`) + li.appendChild(text) //why is the # sign here??? + allPosts.append(li) + }) +}; + +const post5 = () => { + const fivePosts = document.getElementById('five-posts') + //logic to get 5 posts + const li = document.createElement('li') + //const text= document.createTextNode() + li.appendChild(text) + fivePosts.append(li) + +} + +const comments = () => { + const commentsVar2 = document.getElementById('comments') + //logic to display comments + const li= document.createElement('li') + const text = document.createTextNode(`...`) + li.appendChild(text) + commentsVar2.append(li) + +} + +const users = () => { + const usersVar2 = document.getElementById('users') + //logic to display users + const li = document.createElement('li') + const text = document.createTextNode(`${post.userId}`) + li.appendChild(text) + usersVar2.append(li) +} +// Your job now is to follow the functions above and use them as templates to build the functionality the buttons in the index.html file already have laid out in it. This way you can learn how to build fetch requests and work with other apis and become a real developer!! diff --git a/package-lock.json b/package-lock.json index bd07c0e56..9f21138a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1728,15 +1728,14 @@ "version": "github:kevincolten/htmllint-cli#5901ac1f6cd612f484f40c7f4f7515f22a2ba52d", "from": "github:kevincolten/htmllint-cli", "requires": { - "bluebird": "^3.4.7", - "chalk": "^1.1.3", + "bluebird": "^3.5.1", + "chalk": "^2.3.0", "cjson": "^0.5.0", "glob": "^7.1.1", - "htmllint": "^0.6.0", - "liftoff": "^2.3.0", - "promise": "^7.1.1", - "semver": "^5.3.0", - "yargs": "^6.6.0" + "htmllint": "^0.7.0", + "liftoff": "^2.5.0", + "semver": "^5.4.1", + "yargs": "^10.0.3" }, "dependencies": { "ansi-styles": {