Back
Close

Infix Calculator

cehsu
1,340 views

The goal today is to write a function that evaluates a string that contains an arithmetic expression. We will do this by writing an calc function that calculates the expression using a stack.

For example:

  • calc("1 + 2 * (20 / 5 )") -> 9
  • calc("1 + 2 - 3") -> 0
  • calc("1 + 3 + ( ( 4 / 2 ) * ( 8 * 4 ) )") -> 68

See https://medium.freecodecamp.com/zipline-build-a-smart-javascript-calculator-65e21a8b0b0c for additional guidance.

Write the calc function
function calc(string) {
// Your code here:
}
module.exports = calc;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants