Tuesday, May 25, 2021

Javascript Practice 61: JS Hero - Comments

 
Question: 

Write a function median that takes an array of ascending numbers and returns the median of that numbers.

Example: median([1, 2, 10]) should return 2 and median([1, 2, 10, 100]) should return 6.




Answer: 

function median (arr) {

arr.sort(function(a, b){return a-b});

let isVar = arr.length / 2;

if (isVar % 1 == 0) {

return (arr[isVar  - 1] + arr[isVar]) / 2 } else {

return arr[Math.floor(isVar)];

}

};




> declare a function median 
> it has 1 parameter 'arr' 
> the median function will only take arrays as arguments when called 
>>>>> we first need to ensure that the array is in ascending order
> we shall need to sort the array before any other operation is done on the array
> we sort the array using the sort method
> as the name suggests, this method, sorts the elements of an array and returns the sorted array
> the default sort order is ascending
> it first converts elements into strings
> it then compares it's sequences of UTF-16 code units values
> for eg. so 7 comes before 60, but because numbers are converted to strings, '60' comes before '7' in the unicode order  
> sort method is a higher order function 
> when you have to sort something in Javascript, you have to write your own comparing function
> sort method expects as an argument a 'function'
> we insert a function in to the 'sort' method parameter
> this function has 2 parameters: 'a' & 'b'
> we simply subtract b from a like this: a - b and return the value inside the sort method
> this operation will mainly subtract the second element of the array from the first element
> it works as a function that compares the two elements 'a' and 'b'
> so if a - b <= 0; then the sort method will leave a & b unchanged; which means it wont change the index order for a & b
> if a - b > 0; then the sort method will sort b before a 
> if b - a > 0, then the sort method will sort a before b 
> this operation will be conducted on each and every element of the array
> so the first operation, the 'median' function will perform is to sort the array in ascending order
>>>>> after the sorting of the array, we will need to find the median of the array
> we declare a variable 'isVar'
> we initialize it with a value that shall be returned when we divide the length of the array by 2
> whenever we divide a number by 2, then we shall get 2 outputs
> one will be a whole number, which will mean that the array length is even
> one will be a decimal number; which will mean that array length is odd
> this will be the first step in determining if the array has an odd or even number of elements
> we then use if / else statements to proceed with the next step of determining if the array length is odd or even
> based on the conditions mentioned in the if / else statements we can execute specific tasks
> our first if condition is to determine if the array length is even 
> we use the modulo operator on variable 'isVar' like this: ''' isVar % 1 == 0 '''
> now we know that the variable 'isVar' stores the ouput of: the length of an array divided by 2
> now if the variable 'isVar' ; if divided by 1 has a remainder which is equal to 0 then we can be sure that the array length is even in number
> this is because if an even number is divided by 2 then we will always get a whole number
>  ∴ if the condition '' isVar % 1 == 0 ''' is true then we want the following task to be executed
>  (arr[isVar  - 1] + arr[isVar]) / 2 
> now in order to get the median value of an even number we need to add the two middles and then divide it by 2 
> so in our function we need to access the array elements using the number in the 'isVar' variable
> for eg if total number of elements in an array is 10, then the isVar variable will have the stored value of 5
> in order to get the median value for our eg, we shall need both the 5th and the 6th elements and then divide it by 2
> we access the 5th element of the array like this: arr[isVar]
> however the 5th element is actually the 6th element, as array indexes start from 0
> we remember that we need the 6th element too as it becomes a part of the two middles we need
> we then need to access the 5th element to add it to the 6th element
> so we deduct 1 from the 6th element in order to get the 5th element like this: [isVar  - 1]
> we then divide the sum of both the elements by 2 like this: (arr[isVar  - 1] + arr[isVar]) / 2 
> this task will provide the median value if the array length is an even number
> but what if the array length is an odd number
> we then use an else statement as it is the only other option left 
> and we return it with the following task that needs to be executed: arr[Math.floor(isVar)]
> Math.floor function rounds a number downwards to the nearest integer and returns the result
> for eg 7 / 2 = 3.5; then Math.floor(3.5) = 3 
> ∴ if the isVar is a decimal number (which confirms that the length of the array is an odd number) then we can access the median of an array by simply using the rounded down number stored in the variable '''isVar'''
> this will work as the index value of an array starts with 0  

No comments:

Post a Comment