Skip to content

Instantly share code, notes, and snippets.

@wb4r
Last active May 9, 2016 16:57
Show Gist options
  • Save wb4r/e5d090501b4b7b85a235a88ac82ce9e7 to your computer and use it in GitHub Desktop.
Save wb4r/e5d090501b4b7b85a235a88ac82ce9e7 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////
// 1 -- How Would you print the country of the variable city?
////////////////////////////////////////////////
var city = {name: "London", country: "UK"};
// city.country;
////////////////////////////////////////////////
// 2 -- How would you add a property to the same variable of location = Europe?
////////////////////////////////////////////////
var city = {name: "London", country: "UK"}
// city.location = "Europe";
////////////////////////////////////////////////
// 3 -- Do you think there would be any difference by creating objects like these? Would they reach the same outcome?
////////////////////////////////////////////////
var city = {name: "Berlin", country: "Germany"};
// OR/AND
var city = {};
city.name = "Berlin";
city.country = "Germany";
////////////////////////////////////////////////
// 4 -- Do you think there would be any difference by creating objects like these? Would they reach the same outcome?
////////////////////////////////////////////////
var city = new Object();
city.name = "Paris";
city.country = "France";
// OR/AND
var city = {};
city.name = "Paris";
city.country = "France";
////////////////////////////////////////////////
// 5 -- How can you print with console.log the next sentence using the provided object:
// "I am from Spain but I have never been to Barcelona."
////////////////////////////////////////////////
var city = {name: "Barcelona", country: "Spain"};
// console.log("I am from " + city.country + " but I have never been to " + city.name + ".");
////////////////////////////////////////////////
// 6 -- Create an object whose properties have the strings "tomorrow" and "cinema".
// Then create a sentence using both words.
////////////////////////////////////////////////
// var myObject = {word1: "tomorrow", word2: "cinema"};
// console.log("I am meeting a friend " + myObject.word1 + ". We will probably go to the " + myObject.word2 + ".");
////////////////////////////////////////////////
// 7 -- With the given object:
////////////////////////////////////////////////
var myPet = {name: "Dumbo Octopus", class: "Cephalopoda"}
// What do you think the following statements may return?
myPet.name;
myPet["name"];
myPet.class;
myPet["class"];
// the same.
////////////////////////////////////////////////
// 8 -- With the given object:
////////////////////////////////////////////////
var myObject = {prop1: "I think", prop2: "therefore I am"}
// What do you think the following statement may return?
myObject.prop3
// undefined
////////////////////////////////////////////////
// 9 -- I want to get the sum of both properties of both objects. How can I do that?
////////////////////////////////////////////////
var notepad1 = {num: 5};
var notepad2 = {num: 7};
// notepad1.num + notepad2.num
////////////////////////////////////////////////
// 10 -- I want to get the sum of both properties of both objects. How can I do that?
////////////////////////////////////////////////
var notepad1 = {1: 5}
var notepad2 = {one: 7}
// notepad1[1] + notepad2.one
////////////////////////////////////////////////
// 11 -- Create an object with the necessary information of your favorite recipe.
// It should contain at least the name, an array of 4 ingredients and for how many people would you prepare it.
////////////////////////////////////////////////
var myRecipe = {
name: "Cannelloni with Bechamel",
ingredients: ["Minced beef", "Chopped Parsley", "Béchamel Sauce", "Grated Parmesan"],
commensal: 4
}
// How would you iterate through the array of ingredients of the object?
// myRecipe.ingredients.map(function(ing) {
// console.log(ing);
// })
////////////////////////////////////////////////
// 12 -- There has been a fugue in the Reactor and the developers can't find the solution.
// They called you to fix it.
// When you try to create the following object, it is throwing an error, what is going on?
// Try to find out first without debugging.
////////////////////////////////////////////////
var newMission = {
location: "Secret",
danger: "high",
skillsNeeded: "Courage and some JavaScript",
time: "for yesterday!"
attempts: "one",
superHeroSuitProvided: "yes"
}
////////////////////////////////////////////////
// 13 -- Before checking it in the console, what do you think it would return crowdedPub.dayTime?
// And what do you think the crowdedPub object would look like? How many properties would there be?
////////////////////////////////////////////////
var crowdedPub = {
dayTime: "Morning",
dayTime: "Afternoon",
dayTime: "Evening",
dayTime: "Night"
}
////////////////////////////////////////////////
// 14 -- You want the job? Go get it..
////////////////////////////////////////////////
jobOffer = {
next: {
year: {
i: {
will: {
be: {
a: {
developer: {
for: "Google"
}
}
}
}
}
}
}
}
// jobOffer.next.year.i.will.be.a.developer.for
////////////////////////////////////////////////
// 15 -- The finals of the Olympic Prone Rifle Competition per Groups have finished.
// Which team will get the Gold medal (the one with most points)?
////////////////////////////////////////////////
var teamGreen = {
member1: 44,
member2: 57
}
var teamBlue = {
member1: 76,
member2: 22
}
var teamRed = {
member1: 67,
member2: 29
}
// teamGreen with 101
////////////////////////////////////////////////
// 16 -- Create an object called 'library' that contains an array of 3 differnt
// books. The data for the books is provided below:
////////////////////////////////////////////////
Don Quixote - Miguel de Cervantes
Hamlet - William Shakespeare
Romeo and Juliet - William Shakespeare
They are all available in the Library right now. So each book needs 3 properties:
author, title and availability. Use a boolean option for the last property.
////////////////////////////////////////////////
// 17 -- Extract 'Hamlet' and its availability in the Library
////////////////////////////////////////////////
var library = [
{
author: 'Miguel de Cervantes',
title: 'Don Quixote',
available: true
},
{
author: 'William Shakespeare ',
title: 'Hamlet',
available: true
},
{
author: 'William Shakespeare ',
title: 'Romeo and Juliet',
available: true
}];
// library[1].title
// library[1].available
////////////////////////////////////////////////
// 18 -- The Library has temporary transfered Shakespeare's work to a private
// event. According to the programming principle of DRY we don't want to
// modify all his books manually every time a Collection is handed to an
// event like this. How would you fix the missing property assuming that
// where 'Hamlet' goes, the whole collection goes with?
// (modify the Library data base directly)
////////////////////////////////////////////////
var library = [
{
author: 'Miguel de Cervantes',
title: 'Don Quixote',
available: true
},
{
author: 'William Shakespeare ',
title: 'Hamlet',
available: false
},
{
author: 'William Shakespeare ',
title: 'Mcbeth',
available: // ** code goes here **
},
{
author: 'William Shakespeare ',
title: 'Othello',
available: // ** code goes here **
}];
// {
// author: 'William Shakespeare ',
// title: 'Mcbeth',
// available: library[1].available
// },
// {
// author: 'William Shakespeare ',
// title: 'Othello',
// available: library[1].available
// }
////////////////////////////////////////////////
// 19 -- Create a new object using 'new Object' called myObj and with a propery
// called 'prop'. Assign the value of your wish. The property has to be
// declared when creating the object, in one line of code, don't use myObj.prop = "value";
////////////////////////////////////////////////
// var myObj = new Object({prop: "value"});
////////////////////////////////////////////////
// 20 -- Given the object from the previous exercise:
////////////////////////////////////////////////
var myObj = {
prop: "value"
}
// How would you create, in one line and with the same methodology as the previous
// exercise an identical object without hard typing it? To check if you did it
// correctly this two statements have to return 'true':
// myObj == myObj2
// myObj === myObj2
// var myObj2 = new Object(myObj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment