Created
September 1, 2015 20:59
-
-
Save jrobber/6ac539c1328b27e2b94d to your computer and use it in GitHub Desktop.
Javascript Objects and Arrays review
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
/*Data / Information | |
- Make | |
- Change | |
- Move | |
- ?*/ | |
var myClothes = {shirt: "red"}; | |
function IDoStuff(){ | |
var IDoInsideStuff = function(name){ | |
return "Hey " + name; | |
} | |
return IDoInsideStuff; | |
} | |
var result = IDoStuff(); | |
var abc= result("Marco"); | |
//Arrays | |
var numbers = [1,2,3,4,5,6,7,8,9,0]; | |
var evenNumbers = []; //expect [2,4,6,8,0] | |
//for | |
//move the correct data to the new array | |
//Hint: javascript mod | |
//for(var i = 0; i < arr.length; i++){ | |
// //Code here | |
//} | |
//for(var i = arr.length -1; i >= 0; i--){ | |
// | |
//} | |
var myBox = { | |
shirt: "red", | |
ball: "large", | |
book: "Fifty Shades of Yellow", | |
kid: "the bad kid Steve who is 7 and not at dev mountain" | |
} | |
myBox.shoes = "Jordans"; | |
myBox.spoiler = "red and fast"; | |
for(var prop in myBox) { | |
console.log(prop); | |
} | |
var attendee = { | |
nickName: "Examplerrr", | |
realName: "John Examplesmith", | |
plusOne: true, | |
skillLevel: "Expert" | |
} | |
var attendee1 = { | |
nickName: "Examplerrr", | |
realName: "James Examplesmith", | |
plusOne: true, | |
skillLevel: "Expert" | |
} | |
var attendee2 = { | |
nickName: "HackMaster", | |
realName: "Dave Jones", | |
plusOne: true, | |
skillLevel: "Beginner" | |
} | |
var attendee3 = { | |
nickName: "Wordsmith", | |
realName: "Suzie Kanye", | |
plusOne: true, | |
skillLevel: "Expert" | |
} | |
attendee3.nickname | |
var attendees = [attendee, attendee1, attendee2, attendee3]; | |
function countDuplicateNicknames(arrayOfAttendees){ | |
//Hints: square bracket notation, obj.hasOwnProperty('prop'); | |
//Make nickname counting object - var nicknameCount = {} | |
var nicknameCount = {}; | |
//Look through attendees - for loop | |
for(var i = 0; i < arrayOfAttendees.length; i++){ | |
//get their nickname | |
var nickname = arrayOfAttendees[i].nickName; //Examplerrrr, Hackmaster, wordsmith, other? | |
//See if that nickname has already been used | |
//IF it has it | |
if(nicknameCount.hasOwnProperty(nickname)){ | |
//add 1 to the count | |
nicknameCount[nickname]++; | |
} else{ | |
//add the nickname and make it equal 1 | |
nicknameCount[nickname] = 1; | |
} | |
} | |
//return results | |
return nicknameCount; | |
} | |
var countedNicknames = countDuplicateNicknames(attendees); | |
for(var prop in countedNicknames){ | |
console.log("The nickname " + prop + " is registerd " + countedNicknames[prop] + " times."); | |
} | |
/* | |
var result = { | |
[nickname]: count | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment