Last active
August 29, 2018 13:59
-
-
Save xthecapx/6b9d0c9cc84d0e28d63749fe0b8dd44d to your computer and use it in GitHub Desktop.
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
/************* TYPES ***********/ | |
1. Add typing to the following code: | |
let bankAccount = { | |
money: 2000, | |
deposit(value) { | |
this.money += value; | |
} | |
}; | |
let myself = { | |
name: "Max", | |
bankAccount: bankAccount, | |
hobbies: ["Sports", "Cooking"] | |
}; | |
myself.bankAccount.deposit(3000); | |
console.log(myself); | |
/****************** Classes **********************/ | |
// 1. Given the following class. | |
function Car(name) { | |
this.name = name; | |
this.acceleration = 0; | |
this.honk = function() { | |
console.log("Toooooooooot!"); | |
}; | |
this.accelerate = function(speed) { | |
this.acceleration = this.acceleration + speed; | |
} | |
} | |
a. Add the corresponding code to generate a new car with the name given. | |
b. Create two instances of the Car and verify the code. | |
Eg: | |
var car = new Car("BMW"); | |
car.honk(); | |
console.log(car.acceleration); | |
car.accelerate(10); | |
console.log(car.acceleration); | |
// 2. Refactor the following code using classes | |
var baseObject = { | |
width: 0, | |
length: 0 | |
}; | |
var rectangle = Object.create(baseObject); | |
rectangle.width = 5; | |
rectangle.length = 2; | |
rectangle.calcSize = function() { | |
return this.width * this.length; | |
}; | |
console.log(rectangle.calcSize()); | |
// 3. Refactor the following code using classes. | |
var person = { | |
_firstName: "" | |
}; | |
Object.defineProperty(person, "firstName", { | |
get: function () { | |
return this._firstName; | |
}, | |
set: function (value) { | |
if (value.length > 3) { | |
this._firstName = value; | |
} | |
else { | |
this._firstName = ""; | |
} | |
}, | |
enumerable: true, | |
configurable: true | |
}); | |
console.log(person.firstName); | |
person.firstName = "Ma"; | |
console.log(person.firstName); | |
person.firstName = "Maximilian"; | |
console.log(person.firstName); | |
/****************** New in Javascript **********************/ | |
// 1. Refactor the following code using the new javascript features. (Arrow function) | |
var double = function(value) { | |
return value * 2; | |
}; | |
console.log(double(10)); | |
// 2. Use the default value to refactor this code (Default value) | |
var greet = function (name) { | |
if (name === undefined) { name = "Max"; } | |
console.log("Hello, " + name); | |
}; | |
greet(); | |
greet("Anna"); | |
// 3. Concat two arrays using the new javascript features (Spread operator) | |
let numbersE: number[] = [-3, 33, 38, 5]; | |
let newArray = [55, 20]; | |
Array.prototype.push.apply(newArray, numbers); | |
console.log(newArray); | |
// 4. Get the values of each element of the array using the new javascript features. (Destructuring arrays) | |
var testResults = [3.89, 2.99, 1.38]; | |
var result1 = testResults[0]; | |
var result2 = testResults[1]; | |
var result3 = testResults[2]; | |
console.log(result1, result2, result3); | |
// 5. Get the properties values of an object in separated variables using the new javascript features. (Destructuring objects) | |
var scientist = {firstName: "Will", experience: 12}; | |
var firstName = scientist.firstName; | |
var experience = scientist.experience; | |
console.log(firstName, experience); | |
// 6. Create an alias of for the following return value (Alias) | |
const getAddres = () => { | |
return { | |
city: 'city', | |
state: 'state', | |
zip: 'zip' | |
} | |
} | |
console.log(c, s, z); | |
console.log(city); | |
// 5. Create a new object with the same name and a new age key value pair. | |
// What we have | |
const user = {name: 'Shivek Khurana'}; | |
// What we want | |
newUser = { | |
name: 'Shivek Khurana', | |
age: 25 | |
} | |
/****************** Rest operator **********************/ | |
// 1. Refactor the following code using the rest operator and the arrow function. | |
function restOperator(a, b, c) { | |
console.log(arguments[0]); | |
// expected output: 1 | |
console.log(arguments[1]); | |
// expected output: 2 | |
console.log(arguments[2]); | |
// expected output: 3 | |
return anguments.length; | |
} | |
console.log('There are: ' + restOperator(1, 2, 3, 'd') + ' arguments'); | |
// 2. split the followin array in two variables. The firts variable should be 'winter', and the second variable should be ['spring', 'summer'] (Array destructuring) | |
const season = ['winter', 'spring', 'summer']; | |
let [head, ...rest] = season; | |
head; | |
rest; | |
/****************** Using iterators **********************/ | |
// 1. Get the result array using, forEach and map. | |
// What we have | |
const officers = [ | |
{ id: 20, name: 'Captain Piett' }, | |
{ id: 24, name: 'General Veers' }, | |
{ id: 56, name: 'Admiral Ozzel' }, | |
{ id: 88, name: 'Commander Jerjerrod' } | |
]; | |
// What we need | |
const result = [20, 24, 56, 88]; | |
// Solution with old javascript | |
var officersIdsFor = []; | |
for (var i = 0; i < officers.length; i++) { | |
officersIdsForEach.push(officers[i].id); | |
} | |
// 2. Get the total experience of the following collection of pilos. (Tip. use reduce) | |
// What we have | |
const pilots = [ | |
{ | |
id: 10, | |
name: "Poe Dameron", | |
years: 14, | |
}, | |
{ | |
id: 2, | |
name: "Temmin 'Snap' Wexley", | |
years: 30, | |
}, | |
{ | |
id: 41, | |
name: "Tallissan Lintra", | |
years: 16, | |
}, | |
{ | |
id: 99, | |
name: "Ello Asty", | |
years: 22, | |
} | |
]; | |
// Solution using old javascript | |
var cont = 0; | |
for (var i = 0; i < pilots.length; i++) { | |
cont += pilots[i].years; | |
} | |
console.log(cont); | |
// 3. Create the "Devastator" (Use reduce) | |
const constructicons = [ | |
{ | |
name: 'Scrapper', | |
form: 'Freightliner Truck', | |
team: 'Decepticon', | |
bodyPart: 'rightLeg' | |
}, | |
{ | |
name: 'Hook', | |
form: 'Mobile Crane', | |
team: 'Decepticon', | |
bodyPart: 'upperTorso' | |
}, | |
{ | |
name: 'Bonecrusher', | |
form: 'Bulldozer', | |
team: 'Decepticon', | |
bodyPart: 'leftArm' | |
}, | |
{ | |
name: 'Scavenger', | |
form: 'Excavator', | |
team: 'Decepticon', | |
bodyPart: 'rightArm' | |
}, | |
{ | |
name: 'Mixmaster', | |
form: 'Concrete Mixer', | |
team: 'Decepticon', | |
bodyPart: 'leftLeg' | |
}, | |
{ | |
name: 'Long Haul', | |
form: 'Dump Truck', | |
team: 'Decepticon', | |
bodyPart: 'lowerTorso' | |
} | |
]; | |
// We want to create the Devastator | |
const devastator = { | |
name: 'Devastator', | |
team: 'Decepticon', | |
form: { | |
leftArm: "Bonecrusher", | |
leftLeg: "Mixmaster", | |
lowerTorso: "Long Haul", | |
rightArm: "Scavenger", | |
rightLeg: "Scrapper", | |
upperTorso: "Hook" | |
} | |
} | |
// 4. Find the name of the decepticon whichs owns the lowerTorso of the Devastator | |
// Example | |
// Do you know what curry is ? | |
// Do you know what clousure is ? | |
const isEqual = (findString) => { | |
// create a clousure to save the firts function parameters | |
return (filteringObject) => { | |
// due clousure you can access to the findString parameter | |
return findString === filteringObject.name; | |
} | |
}; | |
const isEqualES6 = findString => filteringObject => findString === filteringObject.name; | |
const partRegular = constructicons.filter(item => item.name === 'Long Haul'); | |
const part = constructicons.filter(isEqual('Long Haul')); | |
const partES6 = constructicons.filter(isEqualES6('Long Haul')); | |
console.log(part, partES6, partRegular); | |
// 5. Filter the following array with numbers highest than 1 | |
const userIds = [1, 5, 7, 3, 6]; | |
/****************** The final problem **********************/ | |
// What we have | |
const userObject = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'}; | |
// What we want | |
const userWithoutPassword = {name: 'Shivek Khurana', age: 23 }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment