BODMAS Calculator












0















I'm trying to create a calculator that follows the bodmas rule and trying first with multiplication. When the equals button is hit a string such as "4*5+6*7" is stored in displayValue.
As a first attempt I use reg ex to search the string for the multiplication sign and it's position. When it finds the values to the lhs and rhs and stores that it cal. It then multiplies the values using firstValue, secValue and replaces cal with the result in displayValue. This works for 4*5, but I have to press equals again for it to do 6*7. I tried using a while loop but couldn't get it to work. How do I get it to do all the multiplication parts in the string in one go?



  equals() {
const displayValue = this.state.displayValue.slice(0);
let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
console.log("counter: " +counter);
while (counter > 0) {
let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
console.log("cal: " +cal)
let operPos = cal.search(/*/);
let firstValue = parseFloat(cal.slice(0, operPos));
let secValue = parseFloat(cal.slice(operPos + 1));
let finalCal = firstValue * secValue;
let result = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
this.setState({displayValue: result});
counter = counter -1;
console.log("counterII: " +counter)
}
console.log("new display: " +this.state.displayValue)
}









share|improve this question

























  • Please show an example of input and output.

    – vivek_23
    Nov 25 '18 at 13:58











  • you have one calculation (for two arguments), how can you expect more functionallity than coded? use loop/recurrence

    – xadm
    Nov 25 '18 at 14:38











  • @vivek when displayValue: 1*2-3*4, if I click equals the first time displayValue: 2-3*4, then I click equals again and displayValue: 2-12. What I want is to click equals once and displayValue goes from 1*2-3*4 to 2-12.

    – Valerie
    Nov 26 '18 at 2:16











  • @Valerie so you want to evaluate step by step. I would suggest you to first parse all expressions that have * and / . So, the expression gets reduced to just operands and + and -. Now, reduce this too step by step.

    – vivek_23
    Nov 26 '18 at 6:10
















0















I'm trying to create a calculator that follows the bodmas rule and trying first with multiplication. When the equals button is hit a string such as "4*5+6*7" is stored in displayValue.
As a first attempt I use reg ex to search the string for the multiplication sign and it's position. When it finds the values to the lhs and rhs and stores that it cal. It then multiplies the values using firstValue, secValue and replaces cal with the result in displayValue. This works for 4*5, but I have to press equals again for it to do 6*7. I tried using a while loop but couldn't get it to work. How do I get it to do all the multiplication parts in the string in one go?



  equals() {
const displayValue = this.state.displayValue.slice(0);
let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
console.log("counter: " +counter);
while (counter > 0) {
let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
console.log("cal: " +cal)
let operPos = cal.search(/*/);
let firstValue = parseFloat(cal.slice(0, operPos));
let secValue = parseFloat(cal.slice(operPos + 1));
let finalCal = firstValue * secValue;
let result = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
this.setState({displayValue: result});
counter = counter -1;
console.log("counterII: " +counter)
}
console.log("new display: " +this.state.displayValue)
}









share|improve this question

























  • Please show an example of input and output.

    – vivek_23
    Nov 25 '18 at 13:58











  • you have one calculation (for two arguments), how can you expect more functionallity than coded? use loop/recurrence

    – xadm
    Nov 25 '18 at 14:38











  • @vivek when displayValue: 1*2-3*4, if I click equals the first time displayValue: 2-3*4, then I click equals again and displayValue: 2-12. What I want is to click equals once and displayValue goes from 1*2-3*4 to 2-12.

    – Valerie
    Nov 26 '18 at 2:16











  • @Valerie so you want to evaluate step by step. I would suggest you to first parse all expressions that have * and / . So, the expression gets reduced to just operands and + and -. Now, reduce this too step by step.

    – vivek_23
    Nov 26 '18 at 6:10














0












0








0








I'm trying to create a calculator that follows the bodmas rule and trying first with multiplication. When the equals button is hit a string such as "4*5+6*7" is stored in displayValue.
As a first attempt I use reg ex to search the string for the multiplication sign and it's position. When it finds the values to the lhs and rhs and stores that it cal. It then multiplies the values using firstValue, secValue and replaces cal with the result in displayValue. This works for 4*5, but I have to press equals again for it to do 6*7. I tried using a while loop but couldn't get it to work. How do I get it to do all the multiplication parts in the string in one go?



  equals() {
const displayValue = this.state.displayValue.slice(0);
let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
console.log("counter: " +counter);
while (counter > 0) {
let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
console.log("cal: " +cal)
let operPos = cal.search(/*/);
let firstValue = parseFloat(cal.slice(0, operPos));
let secValue = parseFloat(cal.slice(operPos + 1));
let finalCal = firstValue * secValue;
let result = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
this.setState({displayValue: result});
counter = counter -1;
console.log("counterII: " +counter)
}
console.log("new display: " +this.state.displayValue)
}









share|improve this question
















I'm trying to create a calculator that follows the bodmas rule and trying first with multiplication. When the equals button is hit a string such as "4*5+6*7" is stored in displayValue.
As a first attempt I use reg ex to search the string for the multiplication sign and it's position. When it finds the values to the lhs and rhs and stores that it cal. It then multiplies the values using firstValue, secValue and replaces cal with the result in displayValue. This works for 4*5, but I have to press equals again for it to do 6*7. I tried using a while loop but couldn't get it to work. How do I get it to do all the multiplication parts in the string in one go?



  equals() {
const displayValue = this.state.displayValue.slice(0);
let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
console.log("counter: " +counter);
while (counter > 0) {
let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
console.log("cal: " +cal)
let operPos = cal.search(/*/);
let firstValue = parseFloat(cal.slice(0, operPos));
let secValue = parseFloat(cal.slice(operPos + 1));
let finalCal = firstValue * secValue;
let result = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
this.setState({displayValue: result});
counter = counter -1;
console.log("counterII: " +counter)
}
console.log("new display: " +this.state.displayValue)
}






javascript reactjs algorithm calculator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:18







Valerie

















asked Nov 25 '18 at 13:06









ValerieValerie

585




585













  • Please show an example of input and output.

    – vivek_23
    Nov 25 '18 at 13:58











  • you have one calculation (for two arguments), how can you expect more functionallity than coded? use loop/recurrence

    – xadm
    Nov 25 '18 at 14:38











  • @vivek when displayValue: 1*2-3*4, if I click equals the first time displayValue: 2-3*4, then I click equals again and displayValue: 2-12. What I want is to click equals once and displayValue goes from 1*2-3*4 to 2-12.

    – Valerie
    Nov 26 '18 at 2:16











  • @Valerie so you want to evaluate step by step. I would suggest you to first parse all expressions that have * and / . So, the expression gets reduced to just operands and + and -. Now, reduce this too step by step.

    – vivek_23
    Nov 26 '18 at 6:10



















  • Please show an example of input and output.

    – vivek_23
    Nov 25 '18 at 13:58











  • you have one calculation (for two arguments), how can you expect more functionallity than coded? use loop/recurrence

    – xadm
    Nov 25 '18 at 14:38











  • @vivek when displayValue: 1*2-3*4, if I click equals the first time displayValue: 2-3*4, then I click equals again and displayValue: 2-12. What I want is to click equals once and displayValue goes from 1*2-3*4 to 2-12.

    – Valerie
    Nov 26 '18 at 2:16











  • @Valerie so you want to evaluate step by step. I would suggest you to first parse all expressions that have * and / . So, the expression gets reduced to just operands and + and -. Now, reduce this too step by step.

    – vivek_23
    Nov 26 '18 at 6:10

















Please show an example of input and output.

– vivek_23
Nov 25 '18 at 13:58





Please show an example of input and output.

– vivek_23
Nov 25 '18 at 13:58













you have one calculation (for two arguments), how can you expect more functionallity than coded? use loop/recurrence

– xadm
Nov 25 '18 at 14:38





you have one calculation (for two arguments), how can you expect more functionallity than coded? use loop/recurrence

– xadm
Nov 25 '18 at 14:38













@vivek when displayValue: 1*2-3*4, if I click equals the first time displayValue: 2-3*4, then I click equals again and displayValue: 2-12. What I want is to click equals once and displayValue goes from 1*2-3*4 to 2-12.

– Valerie
Nov 26 '18 at 2:16





@vivek when displayValue: 1*2-3*4, if I click equals the first time displayValue: 2-3*4, then I click equals again and displayValue: 2-12. What I want is to click equals once and displayValue goes from 1*2-3*4 to 2-12.

– Valerie
Nov 26 '18 at 2:16













@Valerie so you want to evaluate step by step. I would suggest you to first parse all expressions that have * and / . So, the expression gets reduced to just operands and + and -. Now, reduce this too step by step.

– vivek_23
Nov 26 '18 at 6:10





@Valerie so you want to evaluate step by step. I would suggest you to first parse all expressions that have * and / . So, the expression gets reduced to just operands and + and -. Now, reduce this too step by step.

– vivek_23
Nov 26 '18 at 6:10












2 Answers
2






active

oldest

votes


















0














The displayValue was not updating in each loop because I assigned it as a const variable. Here's the correct code:



equals() {
let displayValue = this.state.displayValue.slice(0);
let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
console.log("counter: " +counter);
while (counter > 0) {
let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
console.log("cal: " +cal)
let operPos = cal.search(/*/);
let firstValue = parseFloat(cal.slice(0, operPos));
let secValue = parseFloat(cal.slice(operPos + 1));
let finalCal = firstValue * secValue;
displayValue = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
this.setState({displayValue: displayValue})
console.log("result: " +displayValue)
counter = counter -1;
console.log("counterII: " +counter)
}
}


If anyone has a better way to achieve the same thing please let me know.






share|improve this answer































    0














    Why not use the eval() operation? Ex:






    document.getElementById("clickMe").onclick = function(){
    let foo = eval("2*4*2");
    console.log(foo);//16
    return;
    }

    <button id="clickMe">Click for Demo</button>





    It can sometimes even parse a function too!
    EDIT: This answer is no longer valid, as the asker intended to do step by step BEDMAS.

    However, I will leave this on here for future readers.






    share|improve this answer


























    • This would directly give the final answer. OP wants it step wise as discussed in the comments. (P.S- I don't downvote).

      – vivek_23
      Nov 26 '18 at 6:11






    • 1





      Ok, I see. Will try to modify it.

      – bluninja1234
      Nov 26 '18 at 16:29











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467743%2fbodmas-calculator%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The displayValue was not updating in each loop because I assigned it as a const variable. Here's the correct code:



    equals() {
    let displayValue = this.state.displayValue.slice(0);
    let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
    console.log("counter: " +counter);
    while (counter > 0) {
    let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
    console.log("cal: " +cal)
    let operPos = cal.search(/*/);
    let firstValue = parseFloat(cal.slice(0, operPos));
    let secValue = parseFloat(cal.slice(operPos + 1));
    let finalCal = firstValue * secValue;
    displayValue = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
    this.setState({displayValue: displayValue})
    console.log("result: " +displayValue)
    counter = counter -1;
    console.log("counterII: " +counter)
    }
    }


    If anyone has a better way to achieve the same thing please let me know.






    share|improve this answer




























      0














      The displayValue was not updating in each loop because I assigned it as a const variable. Here's the correct code:



      equals() {
      let displayValue = this.state.displayValue.slice(0);
      let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
      console.log("counter: " +counter);
      while (counter > 0) {
      let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
      console.log("cal: " +cal)
      let operPos = cal.search(/*/);
      let firstValue = parseFloat(cal.slice(0, operPos));
      let secValue = parseFloat(cal.slice(operPos + 1));
      let finalCal = firstValue * secValue;
      displayValue = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
      this.setState({displayValue: displayValue})
      console.log("result: " +displayValue)
      counter = counter -1;
      console.log("counterII: " +counter)
      }
      }


      If anyone has a better way to achieve the same thing please let me know.






      share|improve this answer


























        0












        0








        0







        The displayValue was not updating in each loop because I assigned it as a const variable. Here's the correct code:



        equals() {
        let displayValue = this.state.displayValue.slice(0);
        let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
        console.log("counter: " +counter);
        while (counter > 0) {
        let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
        console.log("cal: " +cal)
        let operPos = cal.search(/*/);
        let firstValue = parseFloat(cal.slice(0, operPos));
        let secValue = parseFloat(cal.slice(operPos + 1));
        let finalCal = firstValue * secValue;
        displayValue = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
        this.setState({displayValue: displayValue})
        console.log("result: " +displayValue)
        counter = counter -1;
        console.log("counterII: " +counter)
        }
        }


        If anyone has a better way to achieve the same thing please let me know.






        share|improve this answer













        The displayValue was not updating in each loop because I assigned it as a const variable. Here's the correct code:



        equals() {
        let displayValue = this.state.displayValue.slice(0);
        let counter = displayValue.match(/[d.]+(?=*)*[d.]+/g).length;
        console.log("counter: " +counter);
        while (counter > 0) {
        let cal = displayValue.match(/[d.]+(?=*)*[d.]+/).toString().split("*").join(",");
        console.log("cal: " +cal)
        let operPos = cal.search(/*/);
        let firstValue = parseFloat(cal.slice(0, operPos));
        let secValue = parseFloat(cal.slice(operPos + 1));
        let finalCal = firstValue * secValue;
        displayValue = displayValue.replace(/[d.]+(?=*)*[d.]+/, finalCal);
        this.setState({displayValue: displayValue})
        console.log("result: " +displayValue)
        counter = counter -1;
        console.log("counterII: " +counter)
        }
        }


        If anyone has a better way to achieve the same thing please let me know.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 4:54









        ValerieValerie

        585




        585

























            0














            Why not use the eval() operation? Ex:






            document.getElementById("clickMe").onclick = function(){
            let foo = eval("2*4*2");
            console.log(foo);//16
            return;
            }

            <button id="clickMe">Click for Demo</button>





            It can sometimes even parse a function too!
            EDIT: This answer is no longer valid, as the asker intended to do step by step BEDMAS.

            However, I will leave this on here for future readers.






            share|improve this answer


























            • This would directly give the final answer. OP wants it step wise as discussed in the comments. (P.S- I don't downvote).

              – vivek_23
              Nov 26 '18 at 6:11






            • 1





              Ok, I see. Will try to modify it.

              – bluninja1234
              Nov 26 '18 at 16:29
















            0














            Why not use the eval() operation? Ex:






            document.getElementById("clickMe").onclick = function(){
            let foo = eval("2*4*2");
            console.log(foo);//16
            return;
            }

            <button id="clickMe">Click for Demo</button>





            It can sometimes even parse a function too!
            EDIT: This answer is no longer valid, as the asker intended to do step by step BEDMAS.

            However, I will leave this on here for future readers.






            share|improve this answer


























            • This would directly give the final answer. OP wants it step wise as discussed in the comments. (P.S- I don't downvote).

              – vivek_23
              Nov 26 '18 at 6:11






            • 1





              Ok, I see. Will try to modify it.

              – bluninja1234
              Nov 26 '18 at 16:29














            0












            0








            0







            Why not use the eval() operation? Ex:






            document.getElementById("clickMe").onclick = function(){
            let foo = eval("2*4*2");
            console.log(foo);//16
            return;
            }

            <button id="clickMe">Click for Demo</button>





            It can sometimes even parse a function too!
            EDIT: This answer is no longer valid, as the asker intended to do step by step BEDMAS.

            However, I will leave this on here for future readers.






            share|improve this answer















            Why not use the eval() operation? Ex:






            document.getElementById("clickMe").onclick = function(){
            let foo = eval("2*4*2");
            console.log(foo);//16
            return;
            }

            <button id="clickMe">Click for Demo</button>





            It can sometimes even parse a function too!
            EDIT: This answer is no longer valid, as the asker intended to do step by step BEDMAS.

            However, I will leave this on here for future readers.






            document.getElementById("clickMe").onclick = function(){
            let foo = eval("2*4*2");
            console.log(foo);//16
            return;
            }

            <button id="clickMe">Click for Demo</button>





            document.getElementById("clickMe").onclick = function(){
            let foo = eval("2*4*2");
            console.log(foo);//16
            return;
            }

            <button id="clickMe">Click for Demo</button>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 29 '18 at 6:16

























            answered Nov 26 '18 at 5:01









            bluninja1234bluninja1234

            3710




            3710













            • This would directly give the final answer. OP wants it step wise as discussed in the comments. (P.S- I don't downvote).

              – vivek_23
              Nov 26 '18 at 6:11






            • 1





              Ok, I see. Will try to modify it.

              – bluninja1234
              Nov 26 '18 at 16:29



















            • This would directly give the final answer. OP wants it step wise as discussed in the comments. (P.S- I don't downvote).

              – vivek_23
              Nov 26 '18 at 6:11






            • 1





              Ok, I see. Will try to modify it.

              – bluninja1234
              Nov 26 '18 at 16:29

















            This would directly give the final answer. OP wants it step wise as discussed in the comments. (P.S- I don't downvote).

            – vivek_23
            Nov 26 '18 at 6:11





            This would directly give the final answer. OP wants it step wise as discussed in the comments. (P.S- I don't downvote).

            – vivek_23
            Nov 26 '18 at 6:11




            1




            1





            Ok, I see. Will try to modify it.

            – bluninja1234
            Nov 26 '18 at 16:29





            Ok, I see. Will try to modify it.

            – bluninja1234
            Nov 26 '18 at 16:29


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467743%2fbodmas-calculator%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'