day 15 done

pull/387/head
Jose Linardo 3 years ago
parent 8ed5de2865
commit cad9943764

@ -1,6 +1,6 @@
class Person {
/*class Person {
constructor(firstName, lastName, age, country, city) {
this.firstName = firstName
this.lastName = lastName
@ -103,3 +103,126 @@ console.log(s1.getPersonInfo())
console.log(s2.saySomething())
console.log(s2.getFullName())
console.log(s2.getPersonInfo())
*/
//Exercises Level 1
//1.Create an Animal class. The class will have name, age, color, legs properties and create different methods
class Animal {
constructor(name, age, color, legs){
this.name = name
this.age = age
this.color = color
this.legs = legs
}
getInfo(){
return this.name + ' ' + ' ' + `age: ${this.age} color: ${this.color} legs: ${this.legs} ${this.gender}`
}
}
//2. Create a Dog and Cat child class from the Animal Class.
let dog = new Animal('Firulais', 2, 'white', 4 )
console.log(dog.getInfo())
let cat = new Animal('Tom', 1, 'gray', 4)
console.log(cat.getInfo())
//Exercises Level 2
//1. Override the method you create in Animal class
class Reptile extends Animal {
constructor(name, age, color, legs, gender){
super(name, age, color, legs)
this.gender = gender
}
}
let reptile = new Reptile('Lizard', 1, 'green', 4, 'male')
console.log(reptile)
//Exercises Level 3
//1.Let's try to develop a program which calculate measure of central tendency of a sample(mean, median, mode) and measure of variability(range, variance, standard deviation). In addition to those measures find the min, max, count, percentile, and frequency distribution of the sample. You can create a class called Statistics and create all the functions which do statistical calculations as method for the Statistics class. Check the output below.
ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27, 27, 24, 32, 33, 27, 25, 26, 38, 37, 31, 34, 24, 33, 29, 26]
class Statistics {
mean = (arr) => {
const sum = arr.reduce((a, b) => a + b);
const count = arr.length;
// need to return explicitly
return ` Mean: ${Math.round(sum / count)}`
}
median = (arr) => {
let median;
const sortedArr = arr.sort((a, b) => a - b);
if (arr.length % 2 != 0) {
median = sortedArr[(arr.length - 1) / 2];
} else {
median = (sortedArr[(arr.length / 2) - 1] + sortedArr[arr.length / 2]) / 2;
}
return `Median: ${median}`;
}
mode = (arr) => {
const num = [...new Set(arr)];
const count = num.map((element) =>
arr.filter((number) => number === element).length)
let max = Math.max(...count);
let object = {};
object.mode = num[count.indexOf(max)];
object.count = max;
return object;
}
}
//count, sum, min, max, range
let statistics = new Statistics()
console.log(statistics.mean(ages), statistics.median(ages), statistics.mode(ages))
//2.Create a class 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 also a set of expenses and its description.
ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27, 27, 24, 32, 33, 27, 25, 26, 38, 37, 31, 34, 24, 33, 29, 26]
class PersonAccount {
constructor(firstName, lastName, incomes, expenses) {
this.firstName = firstName;
this.lastName = lastName;
this.incomes = incomes;
this.expenses = expenses;
}
totalIncome() {
const incomeValues = Object.values(this.incomes);
const totalIncome = incomeValues.reduce((a, b) => a + b);
return totalIncome;
}
totalExpense() {
let expenseValues = Object.values(this.expenses);
const totalExpense = expenseValues.reduce((a, b) => a + b);
return totalExpense;
}
accountInfo() {
let incomeKeys = Object.keys(this.incomes);
let formattedIncomes = `${incomeKeys.slice(0, -1).join(', ')} and ${incomeKeys.slice(-1)}`;
let expenseKeys = Object.keys(this.expenses);
let formattedExpenses = `${expenseKeys.slice(0, -1).join(', ')} and ${expenseKeys.slice(-1)}`
let info = `This is ${this.firstName} ${this.lastName}'s account.\nThe user has incomes from ${formattedIncomes}.\nThe total income is ${this.totalIncome()}.\nThe user has expenses from ${formattedExpenses}.\nThe total expense is ${this.totalExpense()}.`
return info;
}
addIncome(source, value) {
this.incomes[`${source}`] = value;
return this.incomes;
}
addExpense(source, value) {
this.expenses[`${source}`] = value;
return this.expenses;
}
}
const account1 = new PersonAccount('Juan', 'Brink', {'salary' : 500, 'online course' : 700, 'part time' : 150}, {'bills' : 150, 'groceries' : 250});
console.log(account1.accountInfo());
console.log(account1.addIncome('flea market', 50));
console.log(account1.addExpense('books', 30));
Loading…
Cancel
Save