- script.js
JavaScript
var expectedUsers = 1000;
var usersByDay = [817, 581, 1370, 752, 1247, 681, 1120, 915, 875, 1341, 757, 610, 812, 741, 1139, 812, 638, 877, 1242, 1159, 1372, 1170, 845, 1289, 515, 1247, 769, 1261, 2805, 1201];
// Drawing the traffic graph
muffin.plot(usersByDay, expectedUsers);
// Summarizing traffic
var totalUsers = 0;
for (var i = 0; i <= usersByDay.length - 1; i++) {
totalUsers += usersByDay[i];
}
// Calculating the average traffic value
var averageUsers = totalUsers / usersByDay.length;
console.log('Average traffic: ' + averageUsers);
if (averageUsers > expectedUsers) {
console.log('Traffic is amazing. Keep up the good work!');
} else {
console.log('Traffic is so-so. You need to try harder!');
}
// Sorting the array
for (var i = 0; i <= usersByDay.length - 2; i++) {
var minValue = usersByDay[i];
for (var j = i + 1; j <= usersByDay.length - 1; j++) {
if (usersByDay[j] < minValue) {
minValue = usersByDay[j];
var swap = usersByDay[i];
usersByDay[i] = minValue;
usersByDay[j] = swap;
}
}
}
// Calculating median
if (usersByDay.length % 2 !== 0) {
var medianIndex = (usersByDay.length - 1) / 2;
var median = usersByDay[medianIndex];
} else {
var leftIndex = usersByDay.length / 2 - 1;
var rightIndex = usersByDay.length / 2;
var median = (usersByDay[leftIndex] + usersByDay[rightIndex]) / 2;
}
console.log('Median traffic: ' + median);
Result
Goalscompleted
- At the end of the program, add a check to make sure that
median / averageUsers < 0.9
. - If the condition is met, log the following message in the console
'Something is fishy here!'
. - Otherwise, log this message in the console
'There are no suspicions of fraud!'
.
Comments