Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9106361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:19:18+00:00 2026-06-17T02:19:18+00:00

I’m working on a ‘Equation Calculator’ but have a few problems. I can now

  • 0

I’m working on a ‘Equation Calculator’ but have a few problems. I can now insert my equation but I want it to be numbers, but not just numbers: I want all the ‘+’ ‘s, the ‘-‘ ‘s, the ‘*’ ‘s and the ‘/’ ‘s to be the right things.

So here is my code until now:


var ligning = prompt("Insert your equation here:");
var newLigning = ligning.split("=");
var sideOne = parseInt(newLigning[0],10);
var sideTwo = parseInt(newLigning[1],10);
var calculator = function() {

};
alert(sideOne);
alert(sideTwo);

To be very specific here is an example:

‘1 + 2 = 3’ should alert 3 and then 3 again (because of the two alerts down in the bottom) – How can I make that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:19:19+00:00Added an answer on June 17, 2026 at 2:19 am

    Instead of :

    var sideOne = parseInt(newLigning[0],10);
    

    You might want to have something of the sort :

    var sideOne = performMath(newLining[0],10);
    

    Where :

    function performMath(equation) {
        //CODE HERE
    }
    

    performMath could be done in a few ways. You need to split the equation and obtain the numbers and the equations between the 2. This can be done recursively!

    I think this could be a good starting point.

    Edit

    Unraleted to the question, if you start taking the operation priority (PEDMAS) into account this is a slightly harder task. The approach is the same, performMath would need to be slightly more complexe. In such a case, I would recomend some sort of array structure for the numbers and another for the equations.

    Edit 2

    Paul S Also made a good point of sanitising the string before getting started. You cannot assume someone will enter an equation. When programming Rich Cook said:

    “Programming today is a race between software engineers striving to
    build bigger and better idiot- proof programs, and the Universe trying
    to produce bigger and better idiots. So far, the Universe is winning.”

    Edit 3

    This little snippet of code should be somewhat helpful… by all means this might not the most efficient code…

    var equation = "1+22-3";
    var operators = "+-*\/"
    
    var currentNumber = "";
    var numbers = new Array();
    var operatorArray = new Array();
    for(var i=0; i<equation.length; i++) {
      var currentChar = equation.charAt(i);
      if(operators.indexOf(currentChar) != -1) {
        operatorArray[operatorArray.length] = (currentChar);
        numbers[numbers.length] = (currentNumber);
        currentNumber = "";
      } else {
        currentNumber = currentNumber + currentChar;
        alert(currentNumber);
      }
    }
    numbers[numbers.length] = currentNumber;
    alert("numbers : " + numbers);
    alert("operators : " + operatorArray);
    

    Now all you have to do is go through the arrays and perform the proper operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.