Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
scale balance algorithm in javascrpt

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"
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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]"]));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda