-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion16.js
64 lines (52 loc) · 1.52 KB
/
question16.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
// Find Mean of array
var readlineSync = require('readline-sync');
function errorLog(x) {
if (x == "") {
console.log("can't be empty, bro.");
} else {
console.log("invalid input. try again.");
}
}
var arrayLength;
function lengthOfArray() {
arrayLength = Number(readlineSync.question("Enter length of array : "));
if (arrayLength != Number(arrayLength || arrayLength == "")) {
errorLog(arrayLength);
lengthOfArray();
} else {
elemInput();
}
}
function elemInput() {
var numArray = [];
var elemValues;
var totalValue;
for(var i = 0; i < arrayLength; i++) {
elemValues = Number(readlineSync.question("enter element " + [i+1] + " : "));
if (elemValues != Number(elemValues || elemValues == "")) {
errorLog(elemValues);
elemInput();
return;
} else {
numArray.push(elemValues);
}
}
console.log(numArray);
const add = (a, b) => a + b;
totalValue = numArray.reduce(add) / arrayLength;
console.log("The mean of array is: " + totalValue);
exit();
}
function exit() {
var exitQuestion = readlineSync.question("Would you like to continue? (Y/n) : ");
if (exitQuestion == "y" || exitQuestion == "Y") {
console.log("here we go again...");
lengthOfArray();
} else if (exitQuestion == "n") {
console.log("Thank you, Goodbye!");
} else {
console.log("invalid answer, try again.");
exit();
}
}
lengthOfArray();