CoreJS: Objects and Properties
27 Jul 2015Codewars
We're going to use community of Codewars as a supplementary resource for collaboratively-filtered coding puzzles ("Kata"). Please make yourself an account, list 'PCS' as your clan, and try the first two Kata!
Objects and Properties
Make an object representing yourself. First declare a variable holding an empty object, then incrementally assign it 3 properties with keys and values of your choice.
Use "object literal notation" to make similar objects for each other person at your table, using a different variable for each.
Make an object for the entire table, stored in variable
table
, with a property for each person. The property name should be the person's name, and its value should be the person's object.Write four "chained" expressions, each referencing one property of each person at the table. Each expression should start with variable
table
and contain no other variables, only property names.
Looping over properties
Write a function
personIsAt(name,tableObj)
which returns a Boolean:true
if a person namedname
is at tabletableObj
, otherwisefalse
.Write a function
peopleAt(tableObj)
which returns a string listing all the names of people at your tabletableObj
. Separate each name with a newline ('\n').Write a function
whoHasKey(tableObj,key)
to return the name of any person at your table (parametertableObj
) who has a key matching parameterkey
.Write a function
whoHasVal(tableObj,val)
to return the name of any person at your table who has a property with valueval
.Bonus Challenge: Modify your functions
whoHasKey
andwhoHasVal
to return a list of all people matching the key or value, respectively.
JSON transmission
Use
JSON.stringify()
to convert your table object (and all its people) to a JSON string. Then have one person per group post that string in a snippet on Slack.Everyone: collect the JSON strings from all four groups and put them together into a single array, representing four tables.
Use your
whoHasVal(table,val)
function to find the name of someone at another table who has a property with a value of your choice.
References
- How many objects remain after the following code runs?
var a={};
var b=a;
var c={a:a,b:b};
a.c = c;
var d=c.a;
delete c.a;
delete c.b;
a = null;
c = null;
Circularity
- Draw the data structure built by the following code:
var loop1;
var loop2;
loop1 = {link : loop2};
loop2 = {link : loop1};
loop1.link.link
- Modify the code to produce two mutually-linked objects.