-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (138 loc) · 4.08 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// === 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) {
return numbers.length;
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the sum of the numbers
*/
function getSum(numbers) {
let sumNum = 0;
for (let i = 0; i < numbers.length; i++) {
sumNum += numbers[i];
}
return sumNum;
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the mean of the numbers
*/
function getMean(numbers) {
let sumNum = 0;
for (let i = 0; i < numbers.length; i++) {
sumNum += numbers[i];
}
return sumNum / numbers.length;
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the smallest of the numbers
*/
function getMin(numbers) {
let smallest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
return smallest;
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the largest of the numbers
*/
function getMax(numbers) {
let largest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
return largest;
}
/**
* @param {number[]} numbers an array of integers
* @returns {number} the range of the numbers (max - min)
*/
function getRange(numbers) {
let largest = numbers[0];
let smallest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
} else if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
return largest - smallest;
}
/**
* @param {number[]} numbers an array of integers
* @returns {number[]} the even numbers in the array
*/
function getEvens(numbers) {
let evens = [];
let evenCount = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
evens[evenCount] = numbers[i];
evenCount++;
}
}
return evens;
}
/**
* @param {number[]} numbers an array of integers
* @returns {number[]} the odd numbers in the array
*/
function getOdds(numbers) {
let odds = [];
let oddCount = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 !== 0) {
odds[oddCount] = numbers[i];
oddCount++;
}
}
return odds;
}
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)}.`);