Thursday, May 20, 2021

Javascript Practice 58: JS Hero - Array: slice()

 
Question: 

Write a function halve that copies the first half of an array. With an odd number of array elements, the middle element should belong to the first half.

Example: halve([1, 2, 3, 4]) should return [1, 2].




Answer: 

function halve (array) {

let half = Math.ceil(array.length / 2); 

return array.slice(0, half);

};




> we declare a variable halve
> it has one parameter - array
> this function will basically copy the first half of an array and return those elements
> in order to divide an array in half, we simply divide the length of an array by 2
> now the array length could be even or odd
> if its even then we get a whole number 
> but if its odd then we get a number with a decimal 
> also if the array is odd then we need to ensure that the middle element is added to the first half
> so in order to solve this we basically first declare a variable half in the function 
> we initialize it with a value array.length / 2
> however, that alone won't work 
> as we need to account for the possibility of the length that could be an odd number 
> furthermore we need to also ensure that if the array length is odd then we want the middle element to be returned in the first half of the array 
> so we use the Math.ceil() method on (array.length/2)
> the Math.ceil() function will always round a number up to the next largest integer
> for eg 1.5 will be 2
> so by doing it we will initialize the variable half by storing the value returned from 
> Math.ceil(array.length / 2)
> if the number is even then the Math.ceil will have no effect 
> if the number is odd then the Math.ceil will round up the decimal to the next highest integer
> this will also take care of the condition that if the length of the array is odd then the middle element will be included in the half variable after the slice function
>>> for eg if we call the function halve([ 'a', 'b', 'c' ])  
>>> then array.length / 2 = 1.5 
>>> Math.ceil(array.length/2) = 2 
>>> half = 2 (and not 1.5, due to the Math.ceil() method)
> note that half has a value of 2 
> we then use the slice method on the array 
> the slice () method does not alter the original array 
> it returns a shallow copy of elements from the original array
> syntax of slice() is (start, end) - 2 arguments
> start: the index at which to start extraction
> end: the extraction will continue up to (but not including) the index number we enter
> we use the slice method to return the first half of the array for our function halve
> like this: array.slice(0, half)
> array.slice is the method
> start: we start extraction at index 0 of the array
> end: instead of the number we enter the variable half as the array could be of any length
> we know that the half has a value of 2; halve([ 'a', 'b', 'c' ]); 3/2 = 1.5; math.ceil = 2;
> our extraction will start at 0 = 'a' and end at 'c' (but not include 'c')
> so we will get 2 elements from the index 0 of the array 
> this will include the element in the position 0 and position 1, which is 'a' & 'b'
> note that 'b' is the middle element of the array
> and hence we shall get the middle element also when we call an array whose length is an odd number

No comments:

Post a Comment