add solutions to day01

pull/344/head
catsAndIvy 2 years ago
parent f8eaa5db1a
commit 027380fdf6

@ -0,0 +1,21 @@
// Exercise 3 - Level 1
const dog = {}
dog.name = 'Montgomery'
dog.legs = 4
dog.colour = 'brown'
dog.age = 2
dog.bark = function () {
console.log('woof woof')
}
dog.breed = 'Pug'
dog.getDog = function () {
console.log(dog)
}
console.log(dog.name, dog.legs, dog.colour, dog.age, dog.breed)
dog.getDog()

@ -0,0 +1,89 @@
// Exercise 3 - Level 2
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
}
}
getMostSkilled = function () {
let maxSkillsPerson = null;
let maxSkillsCount = 0;
Object.keys(users).forEach((person) => {
const skillsCount = users[person].skills.length;
if (skillsCount > maxSkillsCount) {
maxSkillsPerson = person;
maxSkillsCount = skillsCount;
}
});
console.log(maxSkillsPerson);
}
getMostSkilled();
const copyUsers = Object.assign({}, users)
const newUser = {
email: 'newuser@example.com',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 22,
isLoggedIn: true,
points: 35
};
copyUsers['NewUser'] = newUser;
const keys = Object.keys(copyUsers)
const values = Object.values(copyUsers)
const entries = Object.entries(copyUsers)

@ -0,0 +1,106 @@
// Exercise 3 - Level 3
const {users, products} = require('./users-products.js');
const currentDate = new Date()
const personAccount = {
firstName: '',
lastName: '',
incomes: '',
expenses: '',
totalIncome: function () {
},
totalExpense: function () {
},
accountInfo: function () {
},
addIncome: function () {
},
addExpense: function () {
},
accountBalance: function () {
}
}
formatDateTime = function () {
const year = currentDate.getFullYear()
const month = currentDate.getMonth()
const day = currentDate.getDate()
const hours = currentDate.getHours()
const minutes = currentDate.getMinutes()
let amPm = 'AM'
let formatDay = day
let formatMonth = month
if (hours > 11) {
amPm = 'PM'
} else amPm = 'AM'
if (day < 10) {
formatDay = `0${day}`
}
if (month < 10) {
formatMonth = `0${month}`
}
return `${formatDay}/${formatMonth}/${year} ${hours}:${minutes} ${amPm}`
}
// 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.
signUp = function (email, password) {
if (users.some((user) => user.email === email)) {
console.log('An account with this email already exists!');
} else {
const username = email.slice(0, email.indexOf('@'));
const capitalUsername = username[0].toUpperCase + username.slice(1)
newUser = {
_id: 'az17ds',
username: capitalUsernamesername,
email: email,
password: password,
createdAt: formatDateTime(),
isLoggedIn: true,
}
users.push(newUser)
console.log('New user created!')
}
}
function deleteUserByEmail(email) {
const userIndex = users.findIndex((user) => user.email === email);
if (userIndex !== -1) {
users.splice(userIndex, 1);
console.log(`User with email ${email} has been deleted.`);
} else {
console.log('User with email not found.');
}
}
// b. Create a function called signIn which allows user to sign in to the application
const signIn = function (email, password) {
const foundUser = users.find((user) => user.email === email && user.password === password);
if (foundUser) {
foundUser.isLoggedIn = true;
console.log('You have logged in!');
} else {
console.log('Incorrect email or password.');
}
};

@ -0,0 +1,74 @@
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'],
},
]
module.exports = {users, products}
Loading…
Cancel
Save