-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (92 loc) · 2.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// === DO NOT EDIT THIS REGION ===
// Read the comments to understand how the program is structured.
// Prompt the user for a list of integers separated by commas.
const userInputString = prompt(
"Please enter some integers separated by commas.",
"1,2,3,4,5"
);
// Split the string of numbers into an array of strings.
const stringArray = userInputString.split(",");
// Convert the array of strings into an array of numbers.
const numbers = [];
for (let i = 0; i < stringArray.length; i++) {
const str = stringArray[i];
const number = parseInt(str);
numbers.push(number);
}
// Peform some calculations on the numbers.
console.log(numbers);
console.log(`You have given ${getLength(numbers)} numbers.`);
console.log(`The sum of your numbers is ${getSum(numbers)}.`);
console.log(`The mean of your numbers is ${getMean(numbers)}.`);
console.log(`The smallest of your numbers is ${getMin(numbers)}.`);
console.log(`The largest of your numbers is ${getMax(numbers)}.`);
console.log(`The range of your numbers is ${getRange(numbers)}.`);
console.log(`The even numbers you gave are ${getEvens(numbers)}.`);
console.log(`The odd numbers you gave are ${getOdds(numbers)}.`);
// === EDIT THE CODE BELOW ===
// Complete the functions below to make the program work!
/**
* @param {number[]} numbers an array of integers
* @returns {number} the length of the array
*/
function getLength(numbers) {
// TODO
return numbers.length
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the sum of the numbers
*/
function getSum(numbers) {
// TODO
return numbers.reduce((acc, num) => acc + num, 0);
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the mean of the numbers
*/
function getMean(numbers) {
// TODO
return getSum(numbers) / getLength(numbers);
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the smallest of the numbers
*/
function getMin(numbers) {
// TODO
return Math.min(...numbers);
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the largest of the numbers
*/
function getMax(numbers) {
// TODO
return Math.max(numbers);
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the range of the numbers (max - min)
*/
function getRange(numbers) {
// TODO
return getMax(numbers) - getMin(numbers);
}
/**
* @param {number[]} numbers an array of integers
* @returns {number[]} the even numbers in the array
*/
function getEvens(numbers) {
// TODO
return numbers.filter(num => num % 2 === 0);
}
/**
* @param {number[]} numbers an array of integers
* @returns {number[]} the odd numbers in the array
*/
function getOdds(numbers) {
// TODO
return numbers.filter(num => num % 2 !== 0);
}