What are my missing here?

What are my missing here?
0

somebody should help me here, the array is returning [3, 0], where are my missing something?
Given a non-empty array, if there is a place to split the array so that the sum of the numbers on one
side is equal to the sum of the numbers on the other side return the length of the two arrays as an array but
if there is no place to split the array, return -1

canBalance([1, 1, 1, 2, 1]) → [3,2]
canBalance([2, 1, 1, 2, 1]) → -1
canBalance([10, 10]) → [1,1]
function sol(arr){
    let y = 0;
    let c = 0;
    let d = [];
    //let a = []
   // a = arr.sort((a,b) => a - b);

    arr.forEach((x) => y +=x);
    
    for(let i = 0; i < arr.length; i++){
        c += arr[i];
        
        if(c == y/2){
           d.push(arr.splice(0, i+1).length);
           d.push(arr.splice(i+1).length)  
        }  
    }
     return d;
}

const a = [2, 3, 4, 5, 1, 3]
console.log(sol(a))

What exactly does the return value represent?

It returns an array…

I know that, but what do the numbers in the array represent? You did not explain what the array should contain.

this is the task:

Given a non-empty array, if there is a place to split the array so that the sum of the numbers on one
side is equal to the sum of the numbers on the other side return the length of the two arrays as an array but
if there is no place to split the array, return -1

What do the 3 and 2 represent as values in the array?

The function will accept any array as parameter, then look in to the array to see if there is way where if it split such array in to two (not neccessary into two equal half), the addition of one part will equal the addition of the other part…
Now, base on your question, the array passed is [1, 1, 1, 2, 1] and the function look into it and see that if it split the array from index [0-2], its addition will equal to the addition of the remaining indexes. Therefore, it return an comprises of the of each split… i. e index[0-2]=3 , while index[3-4]= 2…
It is this value that the function return as an array

I have landed the first split into the array that the function should return which is “d” in my code, but to add or push the second part of the split to the array is the problem

splice changes the array on which it is called, it will never work
(try to console.log(arr) before and after each of the two d.push(...) you have there)
try an other method - can you think of something else you can use?

or if you want to use splice you need to change the length of what you are getting in the second d.push(...)

1 Like

You do not need to use splice or any other array method inside the if statement code block to solve this problem. You just need to use the value of i in conjunction with arr.length. You also do not need to create any additional arrays (like d).

1 Like

thanks I got it working…thanks for the hint, I got the logic from that statement " (try to console.log(arr) before and after each of the two d.push(...) you have there)"
hence, I change the second push line of code to “d.push(arr.length)” and it work.
but I want to learn, can you explicitly tell me why this " d.push(arr.splice(i+1).length) " is not working, why is it returning Zero. what is really happening there?

thanks for your rapt attention to my call on this problem, this what I am able to come up with and it work as I wanted it…

function sol(arr){
let y = 0;
let c = 0;
let d = ;

arr.forEach((x) => y +=x);

for(let i = 0; i < arr.length; i++){
    c += arr[i];
    
    if(c == y/2){
       d.push(arr.splice(0, i+1).length);
       d.push(arr.length)  
       return d;
    }  
}
return -1;

}

const a = [2, 3, 4, 5, 1, 3]
console.log(sol(a))

please can you now teach me another method I can use, once you said " You do not need to use splice or any other array method inside the if statement code block to solve this problem. You just need to use the value of i in conjunction with arr.length . You also do not need to create any additional arrays (like d ). "

How can I combine i and arr.length to achieve this result, and where would I put if not inside if statement and how can I by-pass the initialization of new array (d).
please be explicit. thanks!

looking at your code
and with this call: canBalance([1, 1, 1, 2, 1])
after the first splice, arr is [2, 1]
so when you splice with arr.splice(i+1), there is nothing at or after index i+1 so you get an empty array, which has length 0

try looking at what happens with this: http://pythontutor.com/javascript.html#mode=edit
you may need to separate each method for clearer view.
Like as below (which is your starting code, just without chained methods)
Look at what happens to arr;

function sol(arr) {
  let y = 0;
  let c = 0;
  let d = [];
  // let a = []
  // a = arr.sort((a,b) => a - b);

  arr.forEach((x) => y += x);

  for (let i = 0; i < arr.length; i++) {
    c += arr[i];

    if (c == y / 2) {
      let valueFromSplice = arr.splice(0, i + 1);
      let length = valueFromSplice.length;
      d.push(length);

      let valueFromSplice2 = arr.splice(i + 1);
      let length2 = valueFromSplice2.length;
      d.push(length2)
    }
  }
  return d;
}

const a = [2, 3, 4, 5, 1, 3]
console.log(sol(a))

you can run it here, with some console.log added, so it is easier to understand what’s happening

Awesome!.. thank you so much am grateful…

hi, kindly help me, the function is not working as expected with this array
const arr = [1, 2, 3, 4, 5, 6, 6, 7, 8];
it returns -1
instead of [6, 3]

function canBalance(arr) {
  const total = arr.reduce((prev, next) => prev + next);
  let cummulativeSum = 0;

  for (let i = 0; i < arr.length; i++) {
    cummulativeSum += arr[i];
    if (cummulativeSum == total / 2) {
      return [i + 1, arr.length - i - 1];
    }
  }
  return -1;
}

I renamed the variables so they describe what the values contain to make the code more readable. You will notice the return statement inside the if statement still returns an array, but just uses the an adjusted value of index (i) and arr.length. If you have questions, let me know.

2 Likes

thanks…am grateful
please can you tell me why this fail, what is happening, where are my missing something, I try it with some other series of array it work as expected, this particular array fail, it works with you code anyway grinning:: so enlighten me on something

function sol(arr){
let y = 0;
let c = 0;
let d = ;

arr.forEach((x) => y +=x);
arr.forEach(
    (i) => {
        c += i
        
         if(c == y/2){
            d.push(arr.splice(0, i+1).length);
            d.push(arr.length)  
            return d;
         } 
    } 
)

return -1;

}
const a = [1, 2, 3, 4, 5, 6, 6, 7, 8]

console.log(sol(a));

In the forEach, the first parameter of the callback function represents the value of the element and not the index. You need to read more about the forEach arguments. Also return statements in the forEach method do not do what you think. Using a return statement inside the forEach will not return the value back to the call of sol(a).

Thank you sir…am grateful, thanks for opening my eye to some salient things

Hi, what are my missing here, this test fail… it expect the function to return -1
but it return an array

function sol(arr){
    const total = arr.reduce((prev, next) => prev + next);
    let cummuSum = 0;

    for (let i = 0; i < arr.length; i++){
        cummuSum +=arr[i];
        if(cummuSum > 0 && cummuSum == total/2){
            return [i+1, arr.length - i - 1];
            
        }
    }
    return -1;
    
} 

const a = [0.1, 0.2, 0.3]
console.log(sol(a))