function ScaleBalancing(strArr) {
const a1 = JSON.parse(strArr[0])[0];
const a2 = JSON.parse(strArr[0])[1];
let weights = JSON.parse(strArr[1]);
if (a1 == a2) {
return 'equal'
}
else {
for (let i = 0; i < weights.length; i++) {
if (a1 + weights[i] === a2 || a2 + weights[i] === a1) {
if (a1 > a2) {
return 'add right side ' + weights[i];
}
else {
return 'add left side ' + weights[i];
}
}
for (let j = i + 1; j < weights.length; j++) {
if (a1 + weights[i] + weights[j] === a2 ||
a2 + weights[i] + weights[j] === a1 ||
a1 + weights[i] === a2 + weights[j] ||
a2 + weights[i] === a1 + weights[j]) {
if (a1 < a2) {
return ' left side add ' + weights[i] + ', right side add ' + weights[j];
}
else {
return ' left side add ' + weights[j] + ', right side add ' + weights[i];
}
}
}
}
}
return 'not possible';
}
console.log(ScaleBalancing(["[4, 4]", "[1, 2,3, 6]"]));
If strArr is ["[5, 9]", "[1, 2, 6, 7]"] then this means there is a balance scale with a weight of 5 on the left side and 9 on the right side. It is possible to balance this scale by adding a 6 to the left side from the list of weights and adding a 2 to the right side. Both scales will now equal 11 and they are perfectly balanced.
So I need to fix this get an output like this:
Input:"[3, 4]", "[1, 2, 7, 7]"
Output:"Left: 1 | Right: 0"
Input:"[13, 4]", "[1, 2, 3, 6, 14]"
Output:"Left: 0 | Right: 3,6"
Input: "[5, 5]", "[1, 2, 3]"
Output: "Equals"
Well it was hard to understand what you wantet to do but i think i got it.
you need the first for loop to run on its own without the scound loop . I've made two loops one in each other to understand better the values, but you can put them together:
function ScaleBalancing(strArr) {
const a1 = JSON.parse(strArr[0])[0];
const a2 = JSON.parse(strArr[0])[1];
let weights = JSON.parse(strArr[1]);
if (a1 == a2) {
return 'equal'
}
else {
for (let i = 0; i < weights.length; i++) {
if (a1 + weights[i] === a2 || a2 + weights[i] === a1) {
if (a1 > a2) {
return 'add right side ' + weights[i];
}
else {
return 'add left side ' + weights[i];
}
}
}
for (let j = 0; j < weights.length; j++) {
for (let i = 0; i < weights.length; i++) {
if (a1 + weights[j] === a2 + weights[i] || a1 + weights[j] === a2 + weights[i]) {
if (a1 > a2) {
return ' left side add ' + weights[i] + ', right side add ' + weights[j];
}
else {
return ' left side add ' + weights[j] + ', right side add ' + weights[i];
}
}
}
}
}
return 'not possible';
}
console.log(ScaleBalancing(["[3,7]", "[1,2,3,6]"]));