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

126
Visualizações
learning about arrays, trying to implement postfix conversion to result

I have the following postfix to convert:

outputArray = [3,4,*,2,9,3,/,4,/,6]

Wanted result is slicedArray = [3, * , 4 ] so I can perform arithmetic operation.

This was my approach which did fail for some reason...

let outputArray = ['3','4','*','2','9','3','/','4','/','6']

outputArray.forEach((element) => {
  while ((
      element !== '*' ||
      element !== '+' ||
      element !== '-' ||
      element !== '/' ||
      element !== '^'
    )) {
    resultArray.push(outputArray.shift(element));
    console.log("Hi Panda " + resultArray)
  }

  if (element.includes("+") ||
    element.includes("-") ||
    element.includes("*") ||
    element.includes("/") ||
    element.includes("^")) {
    let panda = resultArray.length;
    resultArray.splice(panda - 1, 0, element)
    let slicedArray = resultArray.slice(-3)
    console.log(slicedArray);
  }
})

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use a stack and push all value until you got an operator, then add a new array with left and reich value and the operator.

const
    operators = ['+', '-', '*', '/'],
    data = [3, 4, '*', 2, 9, 3, '/', 4, '/', 6],
    stack = [];
    
for (const value of data) {
    if (operators.includes(value)) {
        const
            b = stack.pop(),
            a = stack.pop();
        stack.push([a, value, b]);
        continue;
    }
    stack.push(value);
}

console.log(stack);
.as-console-wrapper { max-height: 100% !important; top: 0; }

An approach to get the value.

const
    operators = { '+': (a, b) => a + b, '-': (a, b) => a - b, '*': (a, b) => a * b, '/': (a, b) => a / b },
    data = [3, 4, '*', 2, 9, 3, '/', 4, '/', 6],
    stack = [];
    
for (const value of data) {
    if (value in operators) {
        const
            b = stack.pop(),
            a = stack.pop();
        stack.push(operators[value](a, b));
        continue;
    }
    stack.push(value);
}

console.log(stack);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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