Home > Enterprise >  How can i access second part of an array?
How can i access second part of an array?

Time:10-29

What i need to do:

// Return an array containing the second half of an array
    // Exclude middle index on odd length arr

My code:

function secondHalf(arr) {
    
    let newArr = [];

    for (let i = Math.floor(arr.length / 2); i >= 0; i--) {
        newArr.push(arr[i]);
    }   return newArr;
}

secondHalf([1, 2]);
secondHalf([1]);

The output i'm getting:

1) Problems
       secondHalf
         should return only the second half the array:

      AssertionError: expected [ 2, 1 ] to deeply equal [ 2 ]
        expected - actual

       [
         2
      -  1
       ]
      
      at Context.<anonymous> (test/problems-specs.js:72:48)
      at processImmediate (node:internal/timers:466:21)

  2) Problems
       secondHalf
         should be the exclusive first half:

      AssertionError: expected [ 1 ] to deeply equal []
        expected - actual

      -[
      -  1
      -]
       []
      
      at Context.<anonymous> (test/problems-specs.js:75:45)
      at processImmediate (node:internal/timers:466:21)

I've tried so many times, and came across methods like .splice() and .slice() but didn't use them because i need to solve it using only loops. What am i doing wrong?

CodePudding user response:

You can use a "normal" for loop with i set to half the length rounded up:

function secondHalf(arr) {

  let newArr = [];

  for (let i = Math.ceil(arr.length / 2); i < arr.length; i  ) {
    newArr.push(arr[i]);
  }

  return newArr;
}

console.log(secondHalf([1, 2, 3, 4, 5, 6, 7]));
console.log(secondHalf([1, 2, 3, 4]));
console.log(secondHalf([1, 2, 3]));
console.log(secondHalf([1, 2]));
console.log(secondHalf([1]));

EDIT: What is happening is that the for loop condition part is divided in three parts:

  • let i = Math.ceil(arr.length / 2);
  • i < arr.length;
  • i

It is the first part that does all "the magic". Instead of starting the iteration from 0 we start at half the array length rounded up, meaning if we have an array like:

[1, 2, 3, 4, 5, 6, 7]
 |  |  |  |  |  |  |
 0  1  2  3  4  5  6 
             |
we start from here

half the length is 3.5 (7 / 2), and rounded up: 4

Meaning we start iterating from index 4 and onwards:

// this is what is happening at each iteration of the for loop:

// i = 4
newArr.push(arr[4]); // which is the number 5
// i = 5
newArr.push(arr[5]); // which is the number 6
// i = 6
newArr.push(arr[6]); // which is the number 7
// i is not below arr.length anymore, so stop iterating

Does that help a little?

CodePudding user response:

function secondHalf(arr) {
    let newArr = []

    for (let i = 1; i <= Math.floor(arr.length / 2); i  ) {
        newArr.push(arr[arr.length-i])
    }
    return newArr
}

secondHalf([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

CodePudding user response:

Try this brother, Here Im checking the length of the array first, if length is not divisible by 2 then removing the mid item by adding 1 and if the length is even then considering the second part by dividing the array length.

function secondHalf(array) {
var secArr = [];
let index = 0;
if(array.length % 2 !== 0) {
    index = parseInt(array.length / 2)  1;
} else index = array.length /2;
for(let i= index; i<array.length; i  ) {
    secArr.push(array[i]);
}
return secArr;

}

secondHalf([1,2,3,4,5])

CodePudding user response:

A cleaner approach:

function secondHalf(arr) {
  const len = arr.length;
  return arr.slice(Math.ceil(len / 2), len);
}

console.log(secondHalf([1, 2, 3, 4, 5, 6]));
console.log(secondHalf([1, 2, 3, 4, 5]));
console.log(secondHalf([1, 2, 3, 4]));
console.log(secondHalf([1]));

  • Related