Completed most object exercises

pull/396/head
Rimvydas Kersys 2 years ago
parent 0f65db9196
commit 8b021ecee2

@ -0,0 +1,15 @@
const countries = [
'Albania',
'Bolivia',
'Canada',
'Denmark',
'Ethiopia',
'Finland',
'Germany',
'Hungary',
'Ireland',
'Japan',
'Kenya',
];
module.exports = {countries};

@ -1,27 +1,3 @@
const countries = [
'Albania',
'Bolivia',
'Canada',
'Denmark',
'Ethiopia',
'Finland',
'Germany',
'Hungary',
'Ireland',
'Japan',
'Kenya',
];
const webTechs = [
'HTML',
'CSS',
'JavaScript',
'React',
'Redux',
'Node',
'MongoDB',
];
// 1 // 1
const array = []; const array = [];

@ -0,0 +1,65 @@
const countries = require('./countries.js').countries;
const webTechs = require('./web_techs.js').webTechs;
Array.prototype.max = function() {
this.sort();
return this[this.length - 1];
}
Array.prototype.min = function() {
this.sort();
return this[0];
}
Array.prototype.median = function() {
this.sort();
const middle = Math.floor(this.length/2);
if (this.length % 2 == 1) {
return this[middle];
}
return (this[middle] + this[middle + 1]) / 2;
}
Array.prototype.sum = function() {
let result = 0;
this.forEach(val => {
result += val;
});
return result;
}
Array.prototype.average = function() {
return this.sum()/this.length;
}
Array.prototype.range = function() {
return this.max() - this.min();
}
const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24];
ages.sort();
console.log("max:", ages.max(), "min:", ages.min());
console.log("median", ages.median());
console.log("average", ages.average());
console.log("range", ages.range());
console.log("lower", Math.abs(ages.min() - ages.average()), "higher", Math.abs(ages.max() - ages.average()));
const firstTen = countries.slice(0, 10);
console.log(firstTen);
console.log(countries.median());
let firstHalf;
let secondHalf;
if (countries.length % 2 == 0) {
firstHalf = countries.slice(0, countries.length / 2);
secondHalf = countries.slice(countries.length/2, countries.length);
} else {
countries.unshift("Country");
}
console.log(firstHalf, secondHalf);

@ -0,0 +1,42 @@
let text = 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.';
// console.log(words);
// console.log(words.length);
let words = text;
for (let code = 33; code <= 47; code++) {
words = words.replaceAll(String.fromCharCode(code), '');
}
words = words.split(' ');
console.log(words);
console.log(words.length);
const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey']
shoppingCart.unshift('Meat');
shoppingCart.push('Sugar');
shoppingCart.splice(shoppingCart.indexOf('Honey'), 1);
shoppingCart[shoppingCart.indexOf('Tea')] = 'Green Tea';
console.log(shoppingCart);
const countries = require("./countries.js");
if (countries.countries.indexOf('Ethiopia') != -1) {
console.log('ETHIOPIA');
} else {
countries.countries.push('Ethiopia');
}
const webTechs = require("./web_techs.js");
if (webTechs.webTechs.indexOf('Sass') != -1) {
console.log('Sass is a CSS preprocess');
} else {
webTechs.webTechs.push('Sass');
console.log(webTechs.webTechs);
}
const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']
const backEnd = ['Node', 'Express', 'MongoDB']
const fullStack = frontEnd.concat(backEnd);
console.log(fullStack);

@ -0,0 +1,11 @@
const webTechs = [
'HTML',
'CSS',
'JavaScript',
'React',
'Redux',
'Node',
'MongoDB',
];
module.exports = {webTechs};

@ -0,0 +1,36 @@
// let userAge = prompt("Enter your age: ");
// if (userAge >= 18)
// confirm("You are old enough to drive.");
// else
// confirm("You are left with " + (18 - userAge) + " to drive.");
// const myAge = 19;
// if (userAge < myAge)
// confirm("You are " + (myAge - userAge) + " younger than me!");
// else if (userAge > myAge)
// confirm("You are " + (userAge - myAge) + " older than me!");
// else
// confirm("You are the same age as me!");
function aGreaterThanB(a, b) {
// if (a > b) {
// console.log("a is greater than b");
// } else {
// console.log("a is less than b");
// }
a > b ?
console.log("a is greater than b") :
console.log("a is less than b");
}
function isEven(value) {
if (value % 2 == 0) {
console.log("The value is even");
return true;
} else {
console.log("The value is odd");
return false;
}
}

@ -0,0 +1,36 @@
function gradeGiver(grade) {
if (grade >= 80) {
return "A";
} else if (grade >= 70) {
return "B";
} else if (grade >= 60) {
return "C";
} else if (grade >= 50) {
return "D";
} else {
return "F";
}
}
function whatSeason(month) {
const seasons = {
"winter": ["december", "january", "february"],
"spring": ["march", "april", "may"],
"summer": ["june", "july", "august"],
"autumn": ["september", "october", "novemeber"]
};
seasons.keys().forEach(key => {
if (seasons[key].find(month.toLowerCase()) != -1) {
return key;
}
});
}
function isWeekend(day) {
if (day.toLowerCase() == "sunday" || day.toLowerCase() == "saturday") {
console.log(day, "is a weekend.");
} else {
console.log(day, "is a working day");
}
}

@ -0,0 +1,21 @@
function daysInMonth(month) {
let months = {
january:31,
february: 28,
march: 31,
april: 30,
may: 31,
june: 30,
july: 31,
august: 31,
september: 30,
october: 31,
novemeber: 30,
december: 31
};
month = month.toLowerCase();
if (month == february && isLeapYear()) {
return months[month] + 1;
}
return months[month];
}

@ -0,0 +1,19 @@
const dog = {};
console.log(dog);
dog.name = "name";
dog.legs = 4;
dog.age = 3;
dog.bark = function() {
return "woof woof";
}
for (const key in dog) {
if (typeof dog[key] == 'function') {
console.log(key, dog[key]());
} else {
console.log(key, dog[key]);
}
}
dog.breed = "labrador";
dog.getDogInfo = function() {
return `This is ${this.name} and he is ${this.age} years old`;
}

@ -0,0 +1,63 @@
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
}
}
function mostSkills() {
let obj = undefined;
for (const person in users) {
if (users.person.skills.length > obj.skills.length) {
obj = users.person;
}
}
console.log(obj);
}
mostSkills();
Loading…
Cancel
Save