- script.js
JavaScript
var usersByDay = [1, 2, 3, 4, 5, 6];
console.log(usersByDay);
if (usersByDay.length % 2 !== 0) {
var medianIndex = (usersByDay.length - 1) / 2;
console.log(medianIndex);
var median = usersByDay[medianIndex];
console.log(median);
}
Result
Goalscompleted
- In the alternative branch of the condition, add the variable
leftIndex
with the valueusersByDay.length / 2 - 1
. - And variable
rightIndex
with valueusersByDay.length / 2
. - Log both variables in the console.
- In the same condition branch, add variable
median
with value(usersByDay[leftIndex] + usersByDay[rightIndex]) / 2
. - Log it in the console as well.
Comments