- 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 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;
}
*/
Result
Goalscompleted
- Uncomment the block with array sorting You can also log the array in the console to ensure that it is sorted correctly.
- Then uncomment the block with median calculation.
- At the end of the program, log message
'Median traffic: ' + median
in the console.
Comments