Set the last number in a string to negative












-3















I have a string with diffrent mathematical characters, and i want to make the last number negative/positive. Let's say the string is "100/5*30-60+333". The result i want is "100/5*30-60+(-333)", and i want to convert it back to positive ("100/5*30-60+333").



function posNeg() {
// hiddenText is a <input> element. This is not shown.
let n = hiddenText.value;
n.split('+');
n.split('-');
n.split('*');
n.split('/');
console.log(n);
}


What i get is the whole hiddenText.value, and not an array of all numbers. Any tips?










share|improve this question























  • But... Why do you want to do that?

    – Islam Elshobokshy
    Jan 2 at 8:17






  • 1





    Would not that be essentially a non-op? I don't understand the converting back part; just keep the original.

    – Drew Reese
    Jan 2 at 8:24
















-3















I have a string with diffrent mathematical characters, and i want to make the last number negative/positive. Let's say the string is "100/5*30-60+333". The result i want is "100/5*30-60+(-333)", and i want to convert it back to positive ("100/5*30-60+333").



function posNeg() {
// hiddenText is a <input> element. This is not shown.
let n = hiddenText.value;
n.split('+');
n.split('-');
n.split('*');
n.split('/');
console.log(n);
}


What i get is the whole hiddenText.value, and not an array of all numbers. Any tips?










share|improve this question























  • But... Why do you want to do that?

    – Islam Elshobokshy
    Jan 2 at 8:17






  • 1





    Would not that be essentially a non-op? I don't understand the converting back part; just keep the original.

    – Drew Reese
    Jan 2 at 8:24














-3












-3








-3








I have a string with diffrent mathematical characters, and i want to make the last number negative/positive. Let's say the string is "100/5*30-60+333". The result i want is "100/5*30-60+(-333)", and i want to convert it back to positive ("100/5*30-60+333").



function posNeg() {
// hiddenText is a <input> element. This is not shown.
let n = hiddenText.value;
n.split('+');
n.split('-');
n.split('*');
n.split('/');
console.log(n);
}


What i get is the whole hiddenText.value, and not an array of all numbers. Any tips?










share|improve this question














I have a string with diffrent mathematical characters, and i want to make the last number negative/positive. Let's say the string is "100/5*30-60+333". The result i want is "100/5*30-60+(-333)", and i want to convert it back to positive ("100/5*30-60+333").



function posNeg() {
// hiddenText is a <input> element. This is not shown.
let n = hiddenText.value;
n.split('+');
n.split('-');
n.split('*');
n.split('/');
console.log(n);
}


What i get is the whole hiddenText.value, and not an array of all numbers. Any tips?







javascript arrays string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 8:15









Elias WennerlundElias Wennerlund

62




62













  • But... Why do you want to do that?

    – Islam Elshobokshy
    Jan 2 at 8:17






  • 1





    Would not that be essentially a non-op? I don't understand the converting back part; just keep the original.

    – Drew Reese
    Jan 2 at 8:24



















  • But... Why do you want to do that?

    – Islam Elshobokshy
    Jan 2 at 8:17






  • 1





    Would not that be essentially a non-op? I don't understand the converting back part; just keep the original.

    – Drew Reese
    Jan 2 at 8:24

















But... Why do you want to do that?

– Islam Elshobokshy
Jan 2 at 8:17





But... Why do you want to do that?

– Islam Elshobokshy
Jan 2 at 8:17




1




1





Would not that be essentially a non-op? I don't understand the converting back part; just keep the original.

– Drew Reese
Jan 2 at 8:24





Would not that be essentially a non-op? I don't understand the converting back part; just keep the original.

– Drew Reese
Jan 2 at 8:24












4 Answers
4






active

oldest

votes


















0















Let's say the string is "100/5*30-60+333". The result i want is
"100/5*30-60+(-333)", and i want to convert it back to positive
("100/5*30-60+333").




The following code does that:



let mathStr = '100/5*30-60+333';
console.log(mathStr);
let tokens = mathStr.split('+');
let index = tokens.length - 1;
let lastToken = tokens[index];
lastToken = '('.concat('-', lastToken, ')');
let newMathStr = tokens[0].concat('+', lastToken);
console.log(newMathStr); // 100/5*30-60+(-333)
console.log(mathStr); // 100/5*30-60+333


EDIT:




... and i want to convert it back to positive ("100/5*30-60+333").




One way is to declare mathStr (with the value "100/5*30-60+333") as a var at the beginning and reuse it, later as you need. Another way is to code as follows:



let str = "100/5*30-60+(-333)";
str = str.replace('(-', '').replace(')', '');
console.log(str); // 100/5*30-60+333





share|improve this answer

































    2














    First, I'd match all of the basic math operators to get their order:



    const operatorsArr = n.match(/+|-|/|*/g)


    Then, split the string:



    function posNeg() {
    // hiddenText is a <input> element. This is not shown.
    let n = hiddenText.value;
    n = n.replace(/+|-|/|*/g, '|');
    n = n.split('|');
    console.log(n);
    }


    Then, you will have an array of numbers, in which you can mutate the last number easily:



    n[n.lengh-1] *= -1;


    Now we can combine the two arrays together:



    let newArr;
    for (let i = 0; i < n.length; i++) {
    newArr.push(n[i]);
    if (operatorsArr[i]) newArr.push(operatorsArr[i]);
    }


    At last, you can rejoin the array to create the new String with a seperator of your choosing. In this example I'm using a space:



    newArr = newArr.join(' ')


    Please let me know how that works out for you.






    share|improve this answer


























    • I believe it is, since he said the problem is that the output of console.log is the full input value.

      – Sagi Rika
      Jan 2 at 8:20











    • Editted the answer, I believe it answers his question now.

      – Sagi Rika
      Jan 2 at 8:48



















    0














    To get numbers You can use replace function and split check code bellow :



       function posNeg() {
    // hiddenText is a <input> element. This is not shown.
    let n = "100/5*30-60+333";
    n = n.replace('+','|+');
    n = n.replace('-','|-');
    n = n.replace('*','|*');
    n = n.replace('/','|/');
    n=n.split('|');console.log(n);
    // to use any caracter from array use it in removeop like example
    // if we have array (split return) have 100 5 30 60 333 we get 100 for example
    // we need to make removeop(n[0]) and that reutrn 100;
    // ok now to replace last value to negative in string you can just make
    // var lastv=n[n.length-1];
    // n[n.length-1] ='(-'+n[n.length-1])+')';
    //var newstring=n.join('');
    //n[n.length-1]=lastv;
    //var oldstring=n.join('');


    }


    function removeop(stringop)
    {

    stringop = stringop.replace('+','');
    stringop = stringop.replace('-','');
    stringop = stringop.replace('*','');
    stringop = stringop.replace('/','');
    return stringop;
    }





    share|improve this answer


























    • Once you've split it, how do you put it back together?

      – Mark Meyer
      Jan 2 at 8:31











    • i updated my code

      – HamzaNig
      Jan 2 at 8:50



















    0














    If you really need to add "()", then you can modify accordingly



    <script>
    function myConversion(){
    var str = "100/5*30-60-333";
    var p = str.lastIndexOf("+");

    if(p>-1)
    {
    str = str.replaceAt(p,"-");
    }
    else
    {
    var n = str.lastIndexOf("-");
    if(n>-1)
    str = str.replaceAt(n,"+");
    }
    console.log(str);
    }
    String.prototype.replaceAt=function(index, replacement) {
    return this.substr(0, index) + replacement+ this.substr(index + replacement.length);

    }
    </script>





    share|improve this answer

























      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%2f54003156%2fset-the-last-number-in-a-string-to-negative%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0















      Let's say the string is "100/5*30-60+333". The result i want is
      "100/5*30-60+(-333)", and i want to convert it back to positive
      ("100/5*30-60+333").




      The following code does that:



      let mathStr = '100/5*30-60+333';
      console.log(mathStr);
      let tokens = mathStr.split('+');
      let index = tokens.length - 1;
      let lastToken = tokens[index];
      lastToken = '('.concat('-', lastToken, ')');
      let newMathStr = tokens[0].concat('+', lastToken);
      console.log(newMathStr); // 100/5*30-60+(-333)
      console.log(mathStr); // 100/5*30-60+333


      EDIT:




      ... and i want to convert it back to positive ("100/5*30-60+333").




      One way is to declare mathStr (with the value "100/5*30-60+333") as a var at the beginning and reuse it, later as you need. Another way is to code as follows:



      let str = "100/5*30-60+(-333)";
      str = str.replace('(-', '').replace(')', '');
      console.log(str); // 100/5*30-60+333





      share|improve this answer






























        0















        Let's say the string is "100/5*30-60+333". The result i want is
        "100/5*30-60+(-333)", and i want to convert it back to positive
        ("100/5*30-60+333").




        The following code does that:



        let mathStr = '100/5*30-60+333';
        console.log(mathStr);
        let tokens = mathStr.split('+');
        let index = tokens.length - 1;
        let lastToken = tokens[index];
        lastToken = '('.concat('-', lastToken, ')');
        let newMathStr = tokens[0].concat('+', lastToken);
        console.log(newMathStr); // 100/5*30-60+(-333)
        console.log(mathStr); // 100/5*30-60+333


        EDIT:




        ... and i want to convert it back to positive ("100/5*30-60+333").




        One way is to declare mathStr (with the value "100/5*30-60+333") as a var at the beginning and reuse it, later as you need. Another way is to code as follows:



        let str = "100/5*30-60+(-333)";
        str = str.replace('(-', '').replace(')', '');
        console.log(str); // 100/5*30-60+333





        share|improve this answer




























          0












          0








          0








          Let's say the string is "100/5*30-60+333". The result i want is
          "100/5*30-60+(-333)", and i want to convert it back to positive
          ("100/5*30-60+333").




          The following code does that:



          let mathStr = '100/5*30-60+333';
          console.log(mathStr);
          let tokens = mathStr.split('+');
          let index = tokens.length - 1;
          let lastToken = tokens[index];
          lastToken = '('.concat('-', lastToken, ')');
          let newMathStr = tokens[0].concat('+', lastToken);
          console.log(newMathStr); // 100/5*30-60+(-333)
          console.log(mathStr); // 100/5*30-60+333


          EDIT:




          ... and i want to convert it back to positive ("100/5*30-60+333").




          One way is to declare mathStr (with the value "100/5*30-60+333") as a var at the beginning and reuse it, later as you need. Another way is to code as follows:



          let str = "100/5*30-60+(-333)";
          str = str.replace('(-', '').replace(')', '');
          console.log(str); // 100/5*30-60+333





          share|improve this answer
















          Let's say the string is "100/5*30-60+333". The result i want is
          "100/5*30-60+(-333)", and i want to convert it back to positive
          ("100/5*30-60+333").




          The following code does that:



          let mathStr = '100/5*30-60+333';
          console.log(mathStr);
          let tokens = mathStr.split('+');
          let index = tokens.length - 1;
          let lastToken = tokens[index];
          lastToken = '('.concat('-', lastToken, ')');
          let newMathStr = tokens[0].concat('+', lastToken);
          console.log(newMathStr); // 100/5*30-60+(-333)
          console.log(mathStr); // 100/5*30-60+333


          EDIT:




          ... and i want to convert it back to positive ("100/5*30-60+333").




          One way is to declare mathStr (with the value "100/5*30-60+333") as a var at the beginning and reuse it, later as you need. Another way is to code as follows:



          let str = "100/5*30-60+(-333)";
          str = str.replace('(-', '').replace(')', '');
          console.log(str); // 100/5*30-60+333






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 12:46

























          answered Jan 2 at 9:28









          prasad_prasad_

          1,5681718




          1,5681718

























              2














              First, I'd match all of the basic math operators to get their order:



              const operatorsArr = n.match(/+|-|/|*/g)


              Then, split the string:



              function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = hiddenText.value;
              n = n.replace(/+|-|/|*/g, '|');
              n = n.split('|');
              console.log(n);
              }


              Then, you will have an array of numbers, in which you can mutate the last number easily:



              n[n.lengh-1] *= -1;


              Now we can combine the two arrays together:



              let newArr;
              for (let i = 0; i < n.length; i++) {
              newArr.push(n[i]);
              if (operatorsArr[i]) newArr.push(operatorsArr[i]);
              }


              At last, you can rejoin the array to create the new String with a seperator of your choosing. In this example I'm using a space:



              newArr = newArr.join(' ')


              Please let me know how that works out for you.






              share|improve this answer


























              • I believe it is, since he said the problem is that the output of console.log is the full input value.

                – Sagi Rika
                Jan 2 at 8:20











              • Editted the answer, I believe it answers his question now.

                – Sagi Rika
                Jan 2 at 8:48
















              2














              First, I'd match all of the basic math operators to get their order:



              const operatorsArr = n.match(/+|-|/|*/g)


              Then, split the string:



              function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = hiddenText.value;
              n = n.replace(/+|-|/|*/g, '|');
              n = n.split('|');
              console.log(n);
              }


              Then, you will have an array of numbers, in which you can mutate the last number easily:



              n[n.lengh-1] *= -1;


              Now we can combine the two arrays together:



              let newArr;
              for (let i = 0; i < n.length; i++) {
              newArr.push(n[i]);
              if (operatorsArr[i]) newArr.push(operatorsArr[i]);
              }


              At last, you can rejoin the array to create the new String with a seperator of your choosing. In this example I'm using a space:



              newArr = newArr.join(' ')


              Please let me know how that works out for you.






              share|improve this answer


























              • I believe it is, since he said the problem is that the output of console.log is the full input value.

                – Sagi Rika
                Jan 2 at 8:20











              • Editted the answer, I believe it answers his question now.

                – Sagi Rika
                Jan 2 at 8:48














              2












              2








              2







              First, I'd match all of the basic math operators to get their order:



              const operatorsArr = n.match(/+|-|/|*/g)


              Then, split the string:



              function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = hiddenText.value;
              n = n.replace(/+|-|/|*/g, '|');
              n = n.split('|');
              console.log(n);
              }


              Then, you will have an array of numbers, in which you can mutate the last number easily:



              n[n.lengh-1] *= -1;


              Now we can combine the two arrays together:



              let newArr;
              for (let i = 0; i < n.length; i++) {
              newArr.push(n[i]);
              if (operatorsArr[i]) newArr.push(operatorsArr[i]);
              }


              At last, you can rejoin the array to create the new String with a seperator of your choosing. In this example I'm using a space:



              newArr = newArr.join(' ')


              Please let me know how that works out for you.






              share|improve this answer















              First, I'd match all of the basic math operators to get their order:



              const operatorsArr = n.match(/+|-|/|*/g)


              Then, split the string:



              function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = hiddenText.value;
              n = n.replace(/+|-|/|*/g, '|');
              n = n.split('|');
              console.log(n);
              }


              Then, you will have an array of numbers, in which you can mutate the last number easily:



              n[n.lengh-1] *= -1;


              Now we can combine the two arrays together:



              let newArr;
              for (let i = 0; i < n.length; i++) {
              newArr.push(n[i]);
              if (operatorsArr[i]) newArr.push(operatorsArr[i]);
              }


              At last, you can rejoin the array to create the new String with a seperator of your choosing. In this example I'm using a space:



              newArr = newArr.join(' ')


              Please let me know how that works out for you.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 2 at 8:53

























              answered Jan 2 at 8:18









              Sagi RikaSagi Rika

              8611




              8611













              • I believe it is, since he said the problem is that the output of console.log is the full input value.

                – Sagi Rika
                Jan 2 at 8:20











              • Editted the answer, I believe it answers his question now.

                – Sagi Rika
                Jan 2 at 8:48



















              • I believe it is, since he said the problem is that the output of console.log is the full input value.

                – Sagi Rika
                Jan 2 at 8:20











              • Editted the answer, I believe it answers his question now.

                – Sagi Rika
                Jan 2 at 8:48

















              I believe it is, since he said the problem is that the output of console.log is the full input value.

              – Sagi Rika
              Jan 2 at 8:20





              I believe it is, since he said the problem is that the output of console.log is the full input value.

              – Sagi Rika
              Jan 2 at 8:20













              Editted the answer, I believe it answers his question now.

              – Sagi Rika
              Jan 2 at 8:48





              Editted the answer, I believe it answers his question now.

              – Sagi Rika
              Jan 2 at 8:48











              0














              To get numbers You can use replace function and split check code bellow :



                 function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = "100/5*30-60+333";
              n = n.replace('+','|+');
              n = n.replace('-','|-');
              n = n.replace('*','|*');
              n = n.replace('/','|/');
              n=n.split('|');console.log(n);
              // to use any caracter from array use it in removeop like example
              // if we have array (split return) have 100 5 30 60 333 we get 100 for example
              // we need to make removeop(n[0]) and that reutrn 100;
              // ok now to replace last value to negative in string you can just make
              // var lastv=n[n.length-1];
              // n[n.length-1] ='(-'+n[n.length-1])+')';
              //var newstring=n.join('');
              //n[n.length-1]=lastv;
              //var oldstring=n.join('');


              }


              function removeop(stringop)
              {

              stringop = stringop.replace('+','');
              stringop = stringop.replace('-','');
              stringop = stringop.replace('*','');
              stringop = stringop.replace('/','');
              return stringop;
              }





              share|improve this answer


























              • Once you've split it, how do you put it back together?

                – Mark Meyer
                Jan 2 at 8:31











              • i updated my code

                – HamzaNig
                Jan 2 at 8:50
















              0














              To get numbers You can use replace function and split check code bellow :



                 function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = "100/5*30-60+333";
              n = n.replace('+','|+');
              n = n.replace('-','|-');
              n = n.replace('*','|*');
              n = n.replace('/','|/');
              n=n.split('|');console.log(n);
              // to use any caracter from array use it in removeop like example
              // if we have array (split return) have 100 5 30 60 333 we get 100 for example
              // we need to make removeop(n[0]) and that reutrn 100;
              // ok now to replace last value to negative in string you can just make
              // var lastv=n[n.length-1];
              // n[n.length-1] ='(-'+n[n.length-1])+')';
              //var newstring=n.join('');
              //n[n.length-1]=lastv;
              //var oldstring=n.join('');


              }


              function removeop(stringop)
              {

              stringop = stringop.replace('+','');
              stringop = stringop.replace('-','');
              stringop = stringop.replace('*','');
              stringop = stringop.replace('/','');
              return stringop;
              }





              share|improve this answer


























              • Once you've split it, how do you put it back together?

                – Mark Meyer
                Jan 2 at 8:31











              • i updated my code

                – HamzaNig
                Jan 2 at 8:50














              0












              0








              0







              To get numbers You can use replace function and split check code bellow :



                 function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = "100/5*30-60+333";
              n = n.replace('+','|+');
              n = n.replace('-','|-');
              n = n.replace('*','|*');
              n = n.replace('/','|/');
              n=n.split('|');console.log(n);
              // to use any caracter from array use it in removeop like example
              // if we have array (split return) have 100 5 30 60 333 we get 100 for example
              // we need to make removeop(n[0]) and that reutrn 100;
              // ok now to replace last value to negative in string you can just make
              // var lastv=n[n.length-1];
              // n[n.length-1] ='(-'+n[n.length-1])+')';
              //var newstring=n.join('');
              //n[n.length-1]=lastv;
              //var oldstring=n.join('');


              }


              function removeop(stringop)
              {

              stringop = stringop.replace('+','');
              stringop = stringop.replace('-','');
              stringop = stringop.replace('*','');
              stringop = stringop.replace('/','');
              return stringop;
              }





              share|improve this answer















              To get numbers You can use replace function and split check code bellow :



                 function posNeg() {
              // hiddenText is a <input> element. This is not shown.
              let n = "100/5*30-60+333";
              n = n.replace('+','|+');
              n = n.replace('-','|-');
              n = n.replace('*','|*');
              n = n.replace('/','|/');
              n=n.split('|');console.log(n);
              // to use any caracter from array use it in removeop like example
              // if we have array (split return) have 100 5 30 60 333 we get 100 for example
              // we need to make removeop(n[0]) and that reutrn 100;
              // ok now to replace last value to negative in string you can just make
              // var lastv=n[n.length-1];
              // n[n.length-1] ='(-'+n[n.length-1])+')';
              //var newstring=n.join('');
              //n[n.length-1]=lastv;
              //var oldstring=n.join('');


              }


              function removeop(stringop)
              {

              stringop = stringop.replace('+','');
              stringop = stringop.replace('-','');
              stringop = stringop.replace('*','');
              stringop = stringop.replace('/','');
              return stringop;
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 2 at 8:50

























              answered Jan 2 at 8:27









              HamzaNigHamzaNig

              810532




              810532













              • Once you've split it, how do you put it back together?

                – Mark Meyer
                Jan 2 at 8:31











              • i updated my code

                – HamzaNig
                Jan 2 at 8:50



















              • Once you've split it, how do you put it back together?

                – Mark Meyer
                Jan 2 at 8:31











              • i updated my code

                – HamzaNig
                Jan 2 at 8:50

















              Once you've split it, how do you put it back together?

              – Mark Meyer
              Jan 2 at 8:31





              Once you've split it, how do you put it back together?

              – Mark Meyer
              Jan 2 at 8:31













              i updated my code

              – HamzaNig
              Jan 2 at 8:50





              i updated my code

              – HamzaNig
              Jan 2 at 8:50











              0














              If you really need to add "()", then you can modify accordingly



              <script>
              function myConversion(){
              var str = "100/5*30-60-333";
              var p = str.lastIndexOf("+");

              if(p>-1)
              {
              str = str.replaceAt(p,"-");
              }
              else
              {
              var n = str.lastIndexOf("-");
              if(n>-1)
              str = str.replaceAt(n,"+");
              }
              console.log(str);
              }
              String.prototype.replaceAt=function(index, replacement) {
              return this.substr(0, index) + replacement+ this.substr(index + replacement.length);

              }
              </script>





              share|improve this answer






























                0














                If you really need to add "()", then you can modify accordingly



                <script>
                function myConversion(){
                var str = "100/5*30-60-333";
                var p = str.lastIndexOf("+");

                if(p>-1)
                {
                str = str.replaceAt(p,"-");
                }
                else
                {
                var n = str.lastIndexOf("-");
                if(n>-1)
                str = str.replaceAt(n,"+");
                }
                console.log(str);
                }
                String.prototype.replaceAt=function(index, replacement) {
                return this.substr(0, index) + replacement+ this.substr(index + replacement.length);

                }
                </script>





                share|improve this answer




























                  0












                  0








                  0







                  If you really need to add "()", then you can modify accordingly



                  <script>
                  function myConversion(){
                  var str = "100/5*30-60-333";
                  var p = str.lastIndexOf("+");

                  if(p>-1)
                  {
                  str = str.replaceAt(p,"-");
                  }
                  else
                  {
                  var n = str.lastIndexOf("-");
                  if(n>-1)
                  str = str.replaceAt(n,"+");
                  }
                  console.log(str);
                  }
                  String.prototype.replaceAt=function(index, replacement) {
                  return this.substr(0, index) + replacement+ this.substr(index + replacement.length);

                  }
                  </script>





                  share|improve this answer















                  If you really need to add "()", then you can modify accordingly



                  <script>
                  function myConversion(){
                  var str = "100/5*30-60-333";
                  var p = str.lastIndexOf("+");

                  if(p>-1)
                  {
                  str = str.replaceAt(p,"-");
                  }
                  else
                  {
                  var n = str.lastIndexOf("-");
                  if(n>-1)
                  str = str.replaceAt(n,"+");
                  }
                  console.log(str);
                  }
                  String.prototype.replaceAt=function(index, replacement) {
                  return this.substr(0, index) + replacement+ this.substr(index + replacement.length);

                  }
                  </script>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 9:12

























                  answered Jan 2 at 9:07









                  Ud_UndefinedUd_Undefined

                  395




                  395






























                      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%2f54003156%2fset-the-last-number-in-a-string-to-negative%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

                      MongoDB - Not Authorized To Execute Command

                      Npm cannot find a required file even through it is in the searched directory

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith