exercise 3 solved

pull/421/head
Fitsumhelina 10 months ago
parent d3b0e3347c
commit 432f9f9e49

@ -1,27 +1,27 @@
// Exercises Level 1 // Exercises Level 1
// Create an Animal class. The class will have name, age, color, legs properties and create different methods // Create an Animal class. The class will have name, age, color, legs properties and create different methods
class animal{ // class animal{
constructor (name, age, color, legs){ // constructor (name, age, color, legs){
this.name = name // this.name = name
this.age = age // this.age = age
this.color = color // this.color = color
this.legs = legs // this.legs = legs
} // }
} // }
// Create a Dog and Cat child class from the Animal Class. // Create a Dog and Cat child class from the Animal Class.
class dog extends { // class dog extends {
constructor (name, age, color, legs){ // constructor (name, age, color, legs){
super(name, age, color, legs) // super(name, age, color, legs)
this.sound = 'woofff' // this.sound = 'woofff'
} // }
}, // },
class cat extends { // class cat extends {
constructor (name, age, color, legs){ // constructor (name, age, color, legs){
super(name, age, color, legs) // super(name, age, color, legs)
this.sound = 'meow' // this.sound = 'meow'
} // }
} // }
// Exercises Level 2 // Exercises Level 2
// Override the method you create in Animal class // Override the method you create in Animal class
@ -33,34 +33,111 @@ class cat extends {
// and measure of variability(range, variance, standard deviation). In addition to those measures find the min, max, count, percentile, // 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 // 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. // calculations as method for the Statistics class. Check the output below.
// class Statistics {
// constructor(data) {
// this.data = data;
// }
// // Mean: The average of all numbers
// mean() {
// return this.data.reduce((acc, cur) => acc + cur) / this.data.length;
// }
// 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] // // Median: The middle value when the numbers are sorted
// median() {
// console.log('Count:', statistics.count()) // 25 // const sorted = [...this.data].sort((a, b) => a - b);
// console.log('Sum: ', statistics.sum()) // 744 // const middle = Math.floor(sorted.length / 2);
// console.log('Min: ', statistics.min()) // 24 // if (sorted.length % 2 === 0) {
// console.log('Max: ', statistics.max()) // 38 // return (sorted[middle - 1] + sorted[middle]) / 2;
// console.log('Range: ', statistics.range() // 14 // } else {
// console.log('Mean: ', statistics.mean()) // 30 // return sorted[middle];
// console.log('Median: ',statistics.median()) // 29 // }
// console.log('Mode: ', statistics.mode()) // {'mode': 26, 'count': 5} // }
// console.log('Variance: ',statistics.var()) // 17.5
// console.log('Standard Deviation: ', statistics.std()) // 4.2 // // Mode: The value that appears most frequently
// console.log('Variance: ',statistics.var()) // 17.5 // mode() {
// console.log('Frequency Distribution: ',statistics.freqDist()) // [(20.0, 26), (16.0, 27), (12.0, 32), (8.0, 37), (8.0, 34), (8.0, 33), (8.0, 31), (8.0, 24), (4.0, 38), (4.0, 29), (4.0, 25)] // const frequency = {};
// let maxFreq = 0;
// console.log(statistics.describe()) // let modes = [];
// // you output should look like this
// this.data.forEach((value) => {
// Count: 25 // frequency[value] = (frequency[value] || 0) + 1;
// Sum: 744 // if (frequency[value] > maxFreq) {
// Min: 24 // maxFreq = frequency[value];
// Max: 38 // }
// Range: 14 // });
// Mean: 30
// Median: 29 // for (const key in frequency) {
// Mode: (26, 5) // if (frequency[key] === maxFreq) {
// Variance: 17.5 // modes.push(Number(key));
// Standard Deviation: 4.2 // }
// Frequency Distribution: [(20.0, 26), (16.0, 27), (12.0, 32), (8.0, 37), (8.0, 34), (8.0, 33), (8.0, 31), (8.0, 24), (4.0, 38), (4.0, 29), (4.0, 25)] // }
// return modes;
// }
// // Range: The difference between the max and min value
// range() {
// return Math.max(...this.data) - Math.min(...this.data);
// }
// // Variance: The average of the squared differences from the mean
// variance() {
// const meanValue = this.mean();
// const squaredDiffs = this.data.map((value) => Math.pow(value - meanValue, 2));
// return squaredDiffs.reduce((acc, cur) => acc + cur) / this.data.length;
// }
// // Standard Deviation: The square root of variance
// standardDeviation() {
// return Math.sqrt(this.variance());
// }
// // Min: The smallest number in the data
// min() {
// return Math.min(...this.data);
// }
// // Max: The largest number in the data
// max() {
// return Math.max(...this.data);
// }
// // Count: The number of values in the data
// count() {
// return this.data.length;
// }
// // Percentile: Returns the value below which a given percentage of data falls
// percentile(percent) {
// const sorted = [...this.data].sort((a, b) => a - b);
// const index = Math.floor(percent / 100 * sorted.length);
// return sorted[index];
// }
// // Frequency Distribution: Shows how frequently each value occurs
// frequencyDistribution() {
// const frequency = {};
// this.data.forEach((value) => {
// frequency[value] = (frequency[value] || 0) + 1;
// });
// return frequency;
// }
// }
// Test the class
const 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];
const stats = new Statistics(ages);
console.log("Mean:", stats.mean()); // Mean
console.log("Median:", stats.median()); // Median
console.log("Mode:", stats.mode()); // Mode
console.log("Range:", stats.range()); // Range
console.log("Variance:", stats.variance()); // Variance
console.log("Standard Deviation:", stats.standardDeviation()); // Standard Deviation
console.log("Min:", stats.min()); // Min
console.log("Max:", stats.max()); // Max
console.log("Count:", stats.count()); // Count
console.log("50th Percentile:", stats.percentile(50)); // 50th Percentile (Median)
console.log("Frequency Distribution:", stats.frequencyDistribution()); // Frequency Distribution

Loading…
Cancel
Save