pull/260/head
parent
6473425eb0
commit
bc35b18aab
@ -0,0 +1,41 @@
|
||||
// For OF Loop
|
||||
// - handy for using with arrays.
|
||||
// - use when NOT interest in the index of an array.
|
||||
// - loops through the values of an iterable object
|
||||
// ex:
|
||||
const numbers = [1,2,3,4,5]; // declare an array of 5 numbers
|
||||
for(const num of numbers){ // assign the values of the numbers array to the const variable 'num'
|
||||
console.log(num); // display every value one at a time
|
||||
}
|
||||
|
||||
//ex:
|
||||
const cities = ['Taylor', 'Austin', 'Houston', 'Dallas', 'Lubbock'];
|
||||
for(const city of cities){
|
||||
console.log(city);
|
||||
}
|
||||
|
||||
// ForEach Loop
|
||||
// - use WHEN interested in the index of an array.
|
||||
// - takes a call back function (element, index, array)
|
||||
// ex:
|
||||
const values = [1,2,3,4,5];
|
||||
values.forEach((element, index)=>{
|
||||
console.log("element:" + element, "index:" + index);
|
||||
});
|
||||
|
||||
|
||||
//For IN Loop
|
||||
// - used for looping through object literals to get keys of objects
|
||||
const user = {
|
||||
firstName: 'Asabeneh',
|
||||
lastName: 'Yetayeh',
|
||||
age: 250,
|
||||
country: 'Finland',
|
||||
skills: ['HTML', 'CSS', 'JS', 'React', 'Node', 'Python', 'D3.js'],
|
||||
}
|
||||
// - key is the key name
|
||||
// - user[key] is the value of the key
|
||||
for(const key in user){
|
||||
console.log("Key: " + key, ", Value: " + user[key]);
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
// Create an empty object called dog
|
||||
const dog = {};
|
||||
// Print the the dog object on the console
|
||||
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
|
||||
dog.name = 'Chester';
|
||||
dog.legs = 4;
|
||||
dog.color = 'Brown';
|
||||
dog.bark = ()=>{
|
||||
return `'woof woof'`;
|
||||
};
|
||||
// Get name, legs, color, age and bark value from the dog object
|
||||
console.log(dog.name,dog.legs,dog.color,dog.bark());
|
||||
// Set new properties the dog object: breed, getDogInfo
|
||||
dog.breed = 'Corgi';
|
||||
dog.getDogInfo = `${dog.name} is a ${dog.color} ${dog.breed} with ${dog.legs} legs and says ${dog.bark()}!`;
|
||||
|
||||
|
@ -0,0 +1,99 @@
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
// Find the person who has many skills in the users object.
|
||||
let people = Object.entries(users); // get key/values pairs of object as an array
|
||||
let obj = {names: [], skill: []}; // create new object to add new properties names and skills which are arrays
|
||||
for(let i = 0; i < people.length; i++){ // loop through array and push names and skills to object
|
||||
obj.names.push(people[i][0]);
|
||||
obj.skill.push(people[i][1].skills.length);
|
||||
}
|
||||
let mostSkills = obj.skill.indexOf(Math.max(...obj.skill)); // find the index position of the skills array, that contains the largest skills set
|
||||
let userMostSkills = obj.names[mostSkills]; // using the index, find name associated with the largest skill set.
|
||||
console.log(`${userMostSkills} has ${Math.max(...obj.skill)} skills, which is the most!`)
|
||||
|
||||
// Count logged in users,count users having greater than equal to 50 points from the following object.
|
||||
let loggedIn = 0, points = 0;
|
||||
for(const people in users){
|
||||
if(users[people].isLoggedIn == true){
|
||||
loggedIn++;
|
||||
}
|
||||
if(users[people].points >= 50){
|
||||
points++;
|
||||
}
|
||||
}
|
||||
console.log(`The number of people logged in is ${loggedIn}.\nThe number of people with 50 points or more is ${points}.`);
|
||||
|
||||
// Find people who are MERN stack developer from the users object
|
||||
let developers = [];
|
||||
for(const people in users){
|
||||
let skills = users[people].skills;
|
||||
switch (true) {
|
||||
case skills.includes('MongoDB') && skills.includes('Express') && skills.includes('React') && skills.includes('Node'):
|
||||
developers.push(people);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
console.log(`The people who are MERN stack developers are ${developers[0]} & ${developers[1]}`);
|
||||
|
||||
// Set your name in the users object without modifying the original users object.
|
||||
let copy = Object.assign({},users);
|
||||
copy.Jaspreet = {};
|
||||
|
||||
// Get all keys or properties of users object.
|
||||
console.log(Object.keys(users));
|
||||
|
||||
// Get all the values of users object.
|
||||
console.log(Object.values(users));
|
@ -0,0 +1,196 @@
|
||||
/*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. */
|
||||
const personalAccount = {
|
||||
firstName: 'Charles',
|
||||
lastName: 'Johnson',
|
||||
incomes: {
|
||||
incomes: [1000,2500,350,5000],
|
||||
descriptions: ['Business profits', 'Salary', 'Copyright on song', 'Apartment for Rent']
|
||||
},
|
||||
expenses: {
|
||||
incomes: [350,123,50,600],
|
||||
descriptions: ['Oil filter change', 'Shoes', 'Takeout order', 'Electric bill']
|
||||
},
|
||||
totalIncome(){},
|
||||
totalExpense(){},
|
||||
accountInfo(){},
|
||||
addIncome(){},
|
||||
addExpense(){},
|
||||
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'],
|
||||
},
|
||||
]
|
||||
|
||||
/* 2. 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 */
|
||||
function signUp(id,name,email,password, date, status){
|
||||
let newUser = {
|
||||
_id: id,
|
||||
username: name,
|
||||
email: email ,
|
||||
password: password,
|
||||
createdAt: date,
|
||||
isLoggedIn: status
|
||||
}
|
||||
users.push(newUser);
|
||||
}
|
||||
signUp('ks28df','Billy','Billy@billy.com','123456','01/2/23 4:14 AM',false);
|
||||
|
||||
// b. Create a function called signIn which allows user to sign in to the application
|
||||
function signIn(username, password){
|
||||
let success = false;
|
||||
users.forEach(element => {
|
||||
if(element.username === username && element.password === password){
|
||||
console.log('Welcome, thank you for signing in!');
|
||||
success = true;
|
||||
}
|
||||
});
|
||||
if(success === false){
|
||||
console.log('Error, invalid username/password');
|
||||
}
|
||||
}
|
||||
signIn('Billy', '123456');
|
||||
|
||||
// 3. The products array has three elements and each of them has six properties.
|
||||
// a. Create a function called rateProduct which rates the product
|
||||
function rateProduct(item, userId, rating) {
|
||||
let myRating = {
|
||||
userId: userId,
|
||||
rate: rating
|
||||
}
|
||||
let success = false;
|
||||
products.forEach(element => {
|
||||
if(element.name.includes(item) && userId.length == 6){
|
||||
element.ratings.push(myRating);
|
||||
success = true;
|
||||
}
|
||||
});
|
||||
if(success == false){
|
||||
console.log('Could not find item or userID incorrect');
|
||||
}
|
||||
}
|
||||
rateProduct('Laptop','ggdksf', 7);
|
||||
|
||||
// b. Create a function called averageRating which calculate the average rating of a product
|
||||
let total = 0, count = 0, success = false;
|
||||
function averageRating(name) {
|
||||
products.forEach(element=>{
|
||||
if(element.name === name && element.ratings.length !== 0){
|
||||
success = true;
|
||||
element.ratings.forEach(itemRate =>{
|
||||
total = total + itemRate.rate
|
||||
count++;
|
||||
})
|
||||
}
|
||||
})
|
||||
if(success == false){
|
||||
console.log(`Item name: '${name}' or rating does not exist`)
|
||||
}else{
|
||||
console.log(`The average rating for ${name} is: ${total/count}`)
|
||||
}
|
||||
}
|
||||
averageRating('mobile phone');
|
||||
|
||||
/* 4. 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.*/
|
||||
function likeProduct(userID, name) {
|
||||
let removed = false, found = false;
|
||||
let likes = userID;
|
||||
products.forEach(element => {
|
||||
if(element.name === name){
|
||||
found = true;
|
||||
element.likes.forEach((element,index,likesArray)=>{
|
||||
if(element === likes){
|
||||
likesArray.splice(index,1);
|
||||
removed = true;
|
||||
}
|
||||
})
|
||||
if(removed == false){
|
||||
element.likes.push(userID);
|
||||
console.log(`Added like for product: ${name}`);
|
||||
}else{
|
||||
console.log(`Deleted like for product: ${name}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
if(found == false){
|
||||
console.log('Error, invalid product name');
|
||||
}
|
||||
}
|
||||
likeProduct('fg12cy', 'Laptop');
|
@ -0,0 +1,43 @@
|
||||
// Object Methods
|
||||
|
||||
// Object assign
|
||||
// - copy an object without modifying the original
|
||||
const person = {
|
||||
firstName: 'Asabeneh',
|
||||
age: 250,
|
||||
country: 'Finland',
|
||||
city: 'Helsinki',
|
||||
skills: ['HTML', 'CSS', 'JS'],
|
||||
title: 'teacher',
|
||||
address: {
|
||||
street: 'Heitamienkatu 16',
|
||||
pobox: 2002,
|
||||
city: 'Helsinki',
|
||||
},
|
||||
getPersonInfo: function () {
|
||||
return `I am ${this.firstName} and I live in ${this.city}, ${this.country}. I am ${this.age}.`
|
||||
}
|
||||
}
|
||||
// target the new object {}, and the source is the original object 'person'
|
||||
const copyObject = Object.assign({}, person);
|
||||
console.log(copyObject);
|
||||
|
||||
// Object keys
|
||||
// - get the keys or properties of an object as an array
|
||||
const keys = Object.keys(copyObject);
|
||||
console.log(keys);
|
||||
|
||||
// Object values
|
||||
// - get values of an object as an array
|
||||
const values = Object.values(copyObject);
|
||||
console.log(values);
|
||||
|
||||
//Object entries
|
||||
// - get keys and values of object as an array
|
||||
const entries = Object.entries(copyObject);
|
||||
console.log(entries);
|
||||
|
||||
// hasOwnProperty
|
||||
// - check to see if object has a key or property that exists
|
||||
console.log(copyObject.hasOwnProperty('age')); // true
|
||||
console.log(copyObject.hasOwnProperty('book')); // false
|
Loading…
Reference in new issue