Last active
April 5, 2023 14:36
-
-
Save DevGW/ba719a7930002811a643817f3a5ef655 to your computer and use it in GitHub Desktop.
Javascript :: Objects II: Examples #js
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
// takes in and adds arrays or amounts and activities | |
function lastFridayNight(transactions) { | |
let mySum = 0; | |
for (let i=0; i < transactions.length; i++) { | |
let transaction = transactions[i]; | |
mySum += transaction.amount; | |
} | |
return mySum; | |
} | |
// refactoring above with Spread functionality: | |
function lastFridayNight(transactions) { | |
let mysum = 0; | |
for (let i=0; i < transactions.length; i++) { | |
const { amount } = transactions[i]; | |
mysum += amount; | |
} | |
return mysum; | |
} | |
// comparing object keys -- need to test both objects against each other | |
function compareObjects(obj1, obj2) { | |
for (let key in obj1) { | |
if (obj1[key] !== obj2[key]) { | |
return false; | |
} | |
} | |
for (let key in obj2) { | |
if (obj2[key] !== obj1[key]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
// translate 1:1 mapping | |
function leetTranslator(myString) { | |
let translation = ''; | |
for (let i=0; i < myString.length; i++) { | |
let currentChar = myString.charAt(i).toLowerCase(); | |
translation += leetChars[letters.indexOf(currentChar)]; | |
} | |
console.log(translation); | |
return translation; | |
} | |
// pulling from nested objects, given array: | |
let animalNoises = [ | |
{ 'dog': { | |
'America' : 'Woof! Woof!', | |
'Germany' : 'Wau Wau!', | |
'England' : 'Bow wow!', | |
'Uruguay' : 'Jua jua!', | |
'Afrikaans' : 'Blaf!', | |
'Korea' : 'Mong mong!', | |
'Iceland' : 'Voff voff!', | |
'Albania' : 'Ham!', | |
'Algeria' : 'Ouaf ouaf!' | |
} | |
}, | |
{ 'cat': { | |
'America' : 'Meow', | |
'Germany' : 'Miauw!', | |
'England' : 'mew mew', | |
'Uruguay' : 'Miau Miau!', | |
'Afrikaans' : 'Purr', | |
'Korea' : 'Nyaong!', | |
'Iceland' : 'Kurnau!', | |
'Albania' : 'Miau', | |
'Algeria' : 'Miaou!' | |
} | |
}, | |
{ 'chicken': { | |
'America' : 'Cluck cluck', | |
'Germany' : 'tock tock tock', | |
'England' : 'Cluck Cluck', | |
'Uruguay' : 'gut gut gdak', | |
'Afrikaans' : 'kukeleku', | |
'Korea' : 'ko-ko-ko', | |
'Iceland' : 'Chuck-chuck!', | |
'Albania' : 'Kotkot', | |
'Algeria' : 'Cotcotcodet' | |
} | |
} | |
]; | |
function petSounds(animal, country) { | |
for (let i = 0; i < animalNoises.length; i++) { | |
let currentObj = animalNoises[i]; | |
let currentSound = currentObj[animal]; | |
if (currentSound) { | |
let animalType = `${animal[0].toUpperCase()}${animal.slice(1)}s`; | |
let animalSound = currentSound[country]; | |
return `${animalType} in ${country} say ${animalSound}`; | |
} | |
} | |
} | |
> [Animals] in [country] say [sound] | |
// create object counting how many occurrences of a certain letter are in a string | |
function frequencyAnalysis(string) { | |
let charFrequencies = {}; | |
for (i = 0; i < string.length; i++) { | |
let currChar = string[i]; | |
if (!charFrequencies[currChar]) { | |
charFrequencies[currChar] = 1; | |
} | |
else { | |
charFrequencies[currChar]++; | |
} | |
} | |
return charFrequencies; | |
} | |
// given an array with nested objects, make new array with day of week attendance per student | |
let classRoom = [ | |
{ | |
"Marnie" : [ | |
{"Monday" : true}, | |
{"Tuesday" : true}, | |
{"Wednesday" : true}, | |
{"Thursday" : true}, | |
{"Friday" : true} | |
] | |
}, | |
{ | |
"Lena" : [ | |
{"Monday" : false}, | |
{"Tuesday" : false}, | |
{"Wednesday" : true}, | |
{"Thursday" : false}, | |
{"Friday" : true} | |
] | |
}, | |
{ | |
"Shoshanna" : [ | |
{"Monday" : true}, | |
{"Tuesday" : true}, | |
{"Wednesday" : false}, | |
{"Thursday" : true}, | |
{"Friday" : false} | |
] | |
}, | |
{ | |
"Jessa" : [ | |
{"Monday" : false}, | |
{"Tuesday" : false}, | |
{"Wednesday" : false}, | |
{"Thursday" : false}, | |
{"Friday" : true} | |
] | |
} | |
]; | |
// YOUR CODE BELOW | |
function attendanceCheck(dayOfWeek) { | |
let studentsPresent = []; | |
for (let i = 0; i < classRoom.length; i++) { | |
let studentObj = classRoom[i]; | |
let studentName = Object.keys(studentObj)[0] | |
let attendanceObjs = studentObj[studentName]; | |
for (let j = 0; j < attendanceObjs.length; j++) { | |
let attendanceObj = attendanceObjs[j]; | |
let dayName = Object.keys(attendanceObj)[0]; | |
let presentOnDay = attendanceObj[dayName]; | |
if (dayName === dayOfWeek && presentOnDay) { | |
studentsPresent.push(studentName); | |
} | |
} | |
} return studentsPresent; | |
} | |
// Object.entries | |
// Object.entries: returns an array of arrays, each array within it contains the key/value pair - allowing us to avoid extra work | |
// so refactor the above to use Object.entries: | |
function attendanceCheck(dayOfWeek) { | |
let studentsPresent = []; | |
for (let i = 0; i < classRoom.length; i++) { | |
let studentObj = classRoom[i]; | |
let [studentName, attendanceObjs] = Object.entries(studentObj)[0] | |
for (let j = 0; j < attendanceObjs.length; j++) { | |
let attendanceObj = attendanceObjs[j]; | |
let [dayName, presentOnDay] = Object.entries(attendanceObj)[0]; | |
if (dayName === dayOfWeek && presentOnDay) { | |
studentsPresent.push(studentName); | |
} | |
} | |
} return studentsPresent; | |
} | |
// writing our own Splice function | |
function mySplice(anArr, startIdx, deleteCount, ...args) { | |
// Store the beginning of the array | |
let startElements = []; | |
// Store the deleted elements | |
let deletedElements = []; | |
// Take everything from before the startIdx and store it in the startElements array | |
for (let i = 0; i < startIdx; i++) { | |
startElements.push(anArr.shift()); | |
} | |
// Then delete everything by removing an element per deleteCount from the new start of the array | |
for (let i = 0; i < deleteCount; i++) { | |
deletedElements.push(anArr.shift()); | |
} | |
// Insert the added elements into the array | |
for (let i = args.length -1; i>=0; i--) { | |
let currentArg = args[i]; | |
anArr.unshift(currentArg); | |
} | |
// reattach the beginning elements onto the array | |
for (let i = startElements.length -1; i>=0; i--) { | |
let currentStartElement = startElements[i]; | |
anArr.unshift(currentStartElement); | |
} | |
// return the deleted elements | |
return deletedElements; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment