You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
8.4 KiB
8.4 KiB
Day 7 - Functions
Exercise:Solutions
Home | << Day 6 | Day 8 >>
Exercise Solutions
Exercises Level 1
- Create an empty object called dog
//app.js
dog = {}
- Print the the dog object on the console
//app.js
dog = {}
console.log(dog)
- Add name, legs, color, age and bark properties for the dog object. The bark property is a method which return woof woof
//app.js
dog = {
name:'garip',
legs:4,
age:6,
color:'black',
bark:'woof woof'
}
console.log(dog)
- Get name, legs, color, age and bark value from the dog object
//app.js
dog = {
name:'garip',
legs:4,
age:6,
color:'black',
bark:'woof woof'
}
console.log(dog.name)
console.log(dog.legs)
console.log(dog['age'])
console.log(dog['color'])
console.log(dog['bark'])
- Set new properties the dog object: breed, getDogInfo
//app.js
dog = {
name:'garip',
legs:4,
age:6,
color:'black',
bark:'woof woof'
}
dog.breed = 'kangal'
console.log(dog.breed)
Exercises Level 2
- Find the person who has many skills in the users object.
//app.js
let maxSkills = 0;
let mostSkilledUser = "";
for (let user in users) {
if (users[user].skills.length > maxSkills) {
maxSkills = users[user].skills.length;
mostSkilledUser = user;
}
}
console.log(mostSkilledUser + " isimli kullanıcının yetenek sayısı: " + maxSkills);
- Count logged in users, count users having greater than equal to 50 points from the following object.
const users = {
Alex: {
email: 'alex@alex.com',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 20,
isLoggedIn: false,
points: 30
},
Asab: {
email: 'asab@asab.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'],
age: 25,
isLoggedIn: false,
points: 50
},
Brook: {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
},
Daniel: {
email: 'daniel@alex.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
age: 20,
isLoggedIn: false,
points: 40
},
John: {
email: 'john@john.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
age: 20,
isLoggedIn: true,
points: 50
},
Thomas: {
email: 'thomas@thomas.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React'],
age: 20,
isLoggedIn: false,
points: 40
},
Paul: {
email: 'paul@paul.com',
skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
age: 20,
isLoggedIn: false,
points: 40
}
}
let pointsIsFifty = 0;
for (let user in users) {
if (users[user].points == 50) {
pointsIsFifty++;
}
}
console.log(pointsIsFifty);
- Find people who are MERN stack developer from the users object
//app.js
let mernStackDevelopers = [];
for (let user in users) {
if (users[user].skills.includes('MongoDB', 'Express', 'React', 'Node')) {
mernStackDevelopers.push(user);
}
}
console.log(mernStackDevelopers)
- Set your name in the users object without modifying the original users object
//app.js
const myName = "Nevzat";
const newUsers = {...users, [myName]: {email: 'bing@microsoft.com', skills: ['search', 'AI'], age: 3, isLoggedIn: true, points: 100}};
console.log(newUsers);
- Get all keys or properties of users object
//app.js
const keys = Object.keys(users);
console.log(keys);
- Get all the values of users object
//app.js
const value = Object.keys(users);
console.log(value);
- Use the countries object to print a country name, capital, populations and languages.
//app.js
for(let country in countries){
console.log(country)
console.log(countries[country].capital)
console.log(countries[country].populations)
console.log(countries[country].langue)
}
Exercises Level 3
- Create an object literal called personAccount. It has firstName, lastName, incomes, expenses properties and it has totalIncome, totalExpense, accountInfo,addIncome, addExpense and accountBalance methods. Incomes is a set of incomes and its description and expenses is a set of incomes and its description.
//app.js
const personAccount = {
firstName: "Nevzat",
lastName: "Atalay",
incomes: [
{description: "Maaş", amount: 5000},
{description: "Kira", amount: 1000},
{description: "Yatırım", amount: 2000}
],
expenses: [
{description: "Kira", amount: 500},
{description: "Yemek", amount: 200},
{description: "Eğlence", amount: 100}
],
totalIncome: function() {
let sum = 0;
for (let i = 0; i < this.incomes.length; i++) {
sum += this.incomes[i].amount;
}
return sum;
},
totalExpense: function() {
let sum = 0;
for (let i = 0; i < this.expenses.length; i++) {
sum += this.expenses[i].amount;
}
return sum;
},
accountInfo: function() {
return "Hesap sahibi: " + this.firstName + " " + this.lastName + "\nToplam gelir: " + this.totalIncome() + "\nToplam gider: " + this.totalExpense();
},
addIncome: function(description, amount) {
this.incomes.push({description: description, amount: amount});
},
addExpense: function(description, amount) {
this.expenses.push({description: description, amount: amount});
},
accountBalance: function() {
return this.totalIncome() - this.totalExpense();
}
};
console.log(personAccount.accountInfo());
personAccount.addIncome("Bonus", 1000);
personAccount.addExpense("Kira", 750);
console.log(personAccount.accountBalance());
- **** Questions:2, 3 and 4 are based on the following two arrays:users and products ()
const users = [
{
_id: 'ab12ex',
username: 'Alex',
email: 'alex@alex.com',
password: '123123',
createdAt:'08/01/2020 9:00 AM',
isLoggedIn: false
},
{
_id: 'fg12cy',
username: 'Asab',
email: 'asab@asab.com',
password: '123456',
createdAt:'08/01/2020 9:30 AM',
isLoggedIn: true
},
{
_id: 'zwf8md',
username: 'Brook',
email: 'brook@brook.com',
password: '123111',
createdAt:'08/01/2020 9:45 AM',
isLoggedIn: true
},
{
_id: 'eefamr',
username: 'Martha',
email: 'martha@martha.com',
password: '123222',
createdAt:'08/01/2020 9:50 AM',
isLoggedIn: false
},
{
_id: 'ghderc',
username: 'Thomas',
email: 'thomas@thomas.com',
password: '123333',
createdAt:'08/01/2020 10:00 AM',
isLoggedIn: false
}
];
const products = [
{
_id: 'eedfcf',
name: 'mobile phone',
description: 'Huawei Honor',
price: 200,
ratings: [
{ userId: 'fg12cy', rate: 5 },
{ userId: 'zwf8md', rate: 4.5 }
],
likes: []
},
{
_id: 'aegfal',
name: 'Laptop',
description: 'MacPro: System Darwin',
price: 2500,
ratings: [],
likes: ['fg12cy']
},
{
_id: 'hedfcg',
name: 'TV',
description: 'Smart TV:Procaster',
price: 400,
ratings: [{ userId: 'fg12cy', rate: 5 }],
likes: ['fg12cy']
}
]
Imagine you are getting the above users collection from a MongoDB database.
a. Create a function called signUp which allows user to add to the collection. If user exists, inform the user that he has already an account.
b. Create a function called signIn which allows user to sign in to the application
-
The products array has three elements and each of them has six properties. a. Create a function called rateProduct which rates the product b. Create a function called averageRating which calculate the average rating of a product
-
Create a function called likeProduct. This function will helps to like to the product if it is not liked and remove like if it was liked.