简体   繁体   中英

Javascript - Factorial from string

I'm trying to make a simple script that calculates the factorials. So for example if you enter "2!+5!+4" you will get "2+120+4". I don't know how you would do it.

function factorial(num) {
      if (num < 0) 
          return -1;
      else if (num == 0) 
        return 1;
      else {
        return (num * factorial(num - 1));
      }
  }

var calcvalue = "2!+5!+4";
var facregex = /(\d)+\!/gi;
var facmatch = calcvalue.match(/(\d)+\!/gi);
var faclength = calcvalue.match(/(\d)+\!/gi).length;
console.log(faclength);

for (var i = 0; i < faclength; i++) {
var calcvalue = calcvalue.replace(facmatch[i], toString(factorial(Number(facmatch[i]))));
} 

console.log("Final: "+calcvalue);

Just replace any occurences of \\d+ followed by ! with the result of the factorial of the \\d+ part (which should be grouped):

var result = mathString.replace(/(\d+)!/g, function(match, number) {
    return factorial(+number);
});

Shorter using an arrow function:

var result = mathString.replace(/(\d+)!/g, (m, n) => factorial(+n));

Example:

 function factorial(num) { if (num <= 1) return 1; return num * factorial(num - 1); } var mathString = "2!+5!+4"; var result = mathString.replace(/(\\d+)!/g, (m, n) => factorial(+n)); console.log(result); 

Note: Don't care whether the factorial bit is preceded by a - or + as it will stay there, hence the numbers retrieved by the regex will always be >= 0 , so the factorial function could be simplified (see the snippett code).

You need to put the + inside the capture group, not after it.

var facregex = /(\d+)!/g;

Both of them match all the digits, but your version only captures the first digit, instead of all the digits.

You don't need a for loop, because you can give a function to the replace() function. It receives the match and all the capture groups as parameters, and returns the replacement.

 var origValue = "2!+10!+4"; var facregex = /(\\d+)!/g; var calcValue = origValue.replace(facregex, function(match, group1) { return factorial(Number(group1)); }); console.log(calcValue); function factorial(num) { if (num < 0) return -1; else if (num == 0) return 1; else { return (num * factorial(num - 1)); } } 

1. Replace the !(Sign) before parsing facmatch[i] to Number .

2. Use eval to get the final calculation from string result .

 function factorial(num) { if (num < 0) return -1; else if (num == 0) return 1; else { return (num * factorial(num - 1)); } } var calcvalue = "2!+5!+4"; var facregex = /(\\d)+\\!/gi; var facmatch = calcvalue.match(/(\\d)+\\!/gi); var faclength = calcvalue.match(/(\\d)+\\!/gi).length; //console.log(faclength,facmatch); for (var i = 0; i < faclength; i++) { var fact=factorial(Number(facmatch[i].replace("!",""))); //console.log("Final: "+fact); calcvalue= calcvalue.replace(facmatch[i], fact); } console.log("Final: "+eval(calcvalue)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM