-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (58 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Complete the Numbers class below
// the constructor has already been provided
class Numbers {
constructor(data){
//data can either be a string or an array of numbers
if (typeof data === "string"){
this.data = str.split(",").map((number) => number * 1);
} else {
this.data = data;
}
}
count() {
//return the count of numbers in data
return this.data.length;
}
printNumbers() {
//print the numbers in data
this.data.forEach((number) => {
console.log(`Number: ${number}`);
});
}
odds() {
//return the odd numbers in data
return this.data.filter(number => number % 2 !== 0);
}
evens() {
//return the even numbers in data
return this.data.filter(number => number % 2 === 0);
}
sum() {
//return the sum of the numbers
return this.data.reduce((total, number) => total + number, 0);
}
product() {
//return the product of the numbers
return this.data.reduce((product, number) => product * number, 1);
}
greaterThan(target){
//return the product greater than the target
return this.data.filter(number => number > target);
}
howMany(target) {
//return the count of a given number
return this.data.filter(number => number === target).length;
}
}
//Prompt the user for a list of integers separated by comas
const str = prompt("enter some numbers, like this ", "1,2,3,3,5,9");
//create a instance of numbers
const n1 = new Numbers(str);
console.log(n1.count()); //returns count of numbers
n1.printNumbers(); //prints the number along with their indexes
console.log(n1.odds()); //returns odd numbers
console.log(n1.evens()); //returns even numbers
console.log(n1.sum()); //returns sum of numbers
console.log(n1.product()); //returns product of numbers
console.log(n1.greaterThan(3)); //returns numbers greater than another number
console.log(n1.howMany(3)); //returns the count of a specific number