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

143
Visualizações
How would you add/subtract/multiply/divide individual elements in an array/list in javascript?

I am developing a simple javascript console calculator, and I need to implement an array/list. My current idea is to create an array with three integers/elements (which the user would fill out), then (depending on what the user wants), the first two elements would be added/subtracted/divided/multiplied, and then the last element would be separately (not dependent on the first operator) added/subtracted/divided/multiplied with the answer from the first two elements.

The current array I have looks like this:

var equfirst = ["num1", "num2", "num3"];

All the elements in the array would be provided by the user.

"num1" and "num2" would be operated on first, then "num3" (as stated earlier). Or, would there be a separate way following the order of operations where the numbers could be operated in no specific order.

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

0

Maybe Reverse Polish Notation can be helpful here?

RPN makes it easy to specify numbers and operators in a sequential way without ever having to use parentheses. A simple JavaScript implementation could be this:

function rpn(inps) { // Reverse Polish Notation calculator
  const st = []; // the stack
  inps.forEach(t => {
    if (t.length==0) return;
    let k = st.length - 2; // index of penultimate element on stack
    if (!isNaN(t)) st.push(+t); // a numeric value (operand)
    else switch (t) { // an operator
      case "+": st[k] += st.pop(); break;
      case "-": st[k] -= st.pop(); break;
      case "*": st[k] *= st.pop(); break;
      case "/": st[k] /= st.pop(); break;
      case "**": st[k] **= st.pop(); break;
    }
  });
  return st.pop()
}

// examples:

const inpArrs=[
  [7,4,"-",5,"*"], // (7-4)*5= 15
  [7,4,5,"*","-"], //  7- 4*5=-13
  [5,7,"+",4,"/"], // (5+7)/4=  3
  // or, something more complicated:
  // Pythagoras's theorem
  [3,3,"*",4,4,"*","+",.5,"**"] // sqrt(3²+4²)=5
 ];

console.log(inpArrs.map(rpn));

// a commented input format:
const eqn="The sum of 3, 4 and 5 in RPN is: 3 4 5 + + = ";

console.log(eqn+rpn(eqn.split(" ")));

As elements of the input array are checked to be either numeric or equal to one of a given list of operators, all other types of input are ignored. Therefore strings can be seen as "comments".

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