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
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))
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
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(...)
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
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))
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;